<s>
XS	B-Language
is	O
a	O
Perl	B-Language
foreign	B-Application
function	I-Application
interface	I-Application
through	O
which	O
a	O
program	O
can	O
call	O
a	O
C	B-Language
or	O
C++	B-Language
subroutine	O
.	O
</s>
<s>
XS	B-Language
or	O
xsub	O
is	O
an	O
abbreviation	O
of	O
"	O
eXternal	O
Subroutine	O
"	O
,	O
where	O
external	O
refers	O
to	O
programming	O
languages	O
external	O
to	O
Perl	B-Language
.	O
</s>
<s>
XS	B-Language
also	O
refers	O
to	O
a	O
glue	O
language	O
for	O
specifying	O
calling	O
interfaces	O
supporting	O
such	O
interfaces	O
(	O
see	O
below	O
)	O
.	O
</s>
<s>
Subroutine	O
libraries	O
in	O
Perl	B-Language
are	O
called	O
modules	O
,	O
and	O
modules	O
that	O
contain	O
xsubs	O
are	O
called	O
XS	B-Language
modules	O
.	O
</s>
<s>
Perl	B-Language
provides	O
a	O
framework	O
for	O
developing	O
,	O
packaging	O
,	O
distributing	O
,	O
and	O
installing	O
modules	O
.	O
</s>
<s>
It	O
may	O
be	O
desirable	O
for	O
a	O
Perl	B-Language
program	O
to	O
invoke	O
a	O
C	B-Language
subroutine	O
in	O
order	O
to	O
handle	O
very	O
CPU	B-Device
or	O
memory	B-Architecture
intensive	O
tasks	O
,	O
to	O
interface	O
with	O
hardware	B-Architecture
or	O
low-level	O
system	O
facilities	O
,	O
or	O
to	O
make	O
use	O
of	O
existing	O
C	B-Language
subroutine	O
libraries	O
.	O
</s>
<s>
The	O
Perl	B-Application
interpreter	I-Application
is	O
a	O
C	B-Language
program	I-Language
,	O
so	O
in	O
principle	O
there	O
is	O
no	O
obstacle	O
to	O
calling	O
from	O
Perl	B-Language
to	O
C	B-Language
.	O
However	O
,	O
the	O
XS	B-Language
interface	O
is	O
complex	O
and	O
highly	O
technical	O
,	O
and	O
using	O
it	O
requires	O
some	O
understanding	O
of	O
the	O
interpreter	B-Application
.	O
</s>
<s>
The	O
earliest	O
reference	O
on	O
the	O
subject	O
was	O
the	O
POD	B-Language
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
write	O
XS	B-Language
modules	O
that	O
wrap	O
C++	B-Language
code	I-Language
.	O
</s>
<s>
The	O
following	O
shows	O
an	O
XS	B-Language
module	O
that	O
exposes	O
a	O
function	O
concat( )	O
to	O
concatenate	O
two	O
strings	O
(	O
i.e.	O
,	O
the	O
equivalent	O
of	O
Perl	B-Language
’s	O
.	O
</s>
<s>
After	O
then	O
follow	O
any	O
number	O
of	O
plain	O
C	B-Language
functions	O
that	O
are	O
callable	O
locally	O
.	O
</s>
<s>
The	O
section	O
that	O
starts	O
with	O
MODULE	O
=	O
Demo::XSModule	O
defines	O
the	O
Perl	B-Language
interface	O
to	O
this	O
code	O
using	O
the	O
actual	O
XS	B-Language
macro	O
language	O
.	O
</s>
<s>
Note	O
that	O
the	O
C	B-Language
code	O
under	O
the	O
CODE	O
:	O
section	O
calls	O
the	O
_do_sv_catsv( )	O
pure-C	O
function	O
that	O
was	O
defined	O
in	O
the	O
prior	O
section	O
.	O
</s>
<s>
Perl	B-Language
’s	O
documentation	O
explains	O
the	O
meaning	O
and	O
purpose	O
of	O
all	O
of	O
the	O
“	O
special	O
”	O
symbols	O
(	O
e.g.	O
,	O
aTHX_	O
and	O
RETVAL	O
)	O
shown	O
above	O
.	O
</s>
<s>
To	O
make	O
this	O
module	O
available	O
to	O
Perl	B-Language
it	O
must	O
be	O
compiled	O
.	O
</s>
<s>
(	O
To	O
build	O
manually	O
:	O
the	O
tool	O
parses	O
an	O
XS	B-Language
module	O
and	O
outputs	O
C	B-Language
source	O
code	O
;	O
that	O
source	O
code	O
is	O
then	O
compiled	O
to	O
a	O
shared	O
library	O
and	O
placed	O
in	O
a	O
directory	O
where	O
Perl	B-Language
can	O
find	O
it	O
.	O
)	O
</s>
<s>
Perl	B-Language
code	O
then	O
uses	O
a	O
module	O
like	O
to	O
load	O
the	O
compiled	O
XS	B-Language
module	O
.	O
</s>
<s>
At	O
this	O
point	O
Perl	B-Language
can	O
call	O
Demo::XSModule::concat('foo', 'bar' )	O
and	O
receive	O
back	O
a	O
string	O
foobar	O
,	O
as	O
if	O
concat( )	O
were	O
itself	O
written	O
in	O
Perl	B-Language
.	O
</s>
<s>
Note	O
that	O
,	O
for	O
building	O
Perl	B-Language
interfaces	O
to	O
preexisting	O
C	B-Language
libraries	O
,	O
the	O
h2xs	O
can	O
automate	O
much	O
of	O
the	O
creation	O
of	O
the	O
XS	B-Language
file	O
itself	O
.	O
</s>
<s>
Creation	O
and	O
maintenance	O
of	O
XS	B-Language
modules	O
requires	O
expertise	O
with	O
C	B-Language
itself	O
as	O
well	O
as	O
Perl	B-Language
’s	O
extensive	O
C	B-Language
API	O
.	O
</s>
<s>
XS	B-Language
modules	O
may	O
only	O
be	O
installed	O
if	O
a	O
C	B-Language
compiler	O
and	O
the	O
header	B-Language
files	I-Language
that	O
the	O
Perl	B-Application
interpreter	I-Application
was	O
compiled	O
against	O
are	O
available	O
.	O
</s>
<s>
Also	O
,	O
new	O
versions	O
of	O
Perl	B-Language
may	O
break	O
binary	B-General_Concept
compatibility	I-General_Concept
requiring	O
XS	B-Language
modules	O
to	O
be	O
recompiled	O
.	O
</s>
