HOME

TheInfoList



OR:

XS is a
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
foreign function interface A foreign function interface (FFI) is a mechanism by which a program written in one programming language can call routines or make use of services written in another. Naming The term comes from the specification for Common Lisp, which explicit ...
through which a program can call a C or
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
subroutine In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Functions may ...
. XS or xsub is an
abbreviation An abbreviation (from Latin ''brevis'', meaning ''short'') is a shortened form of a word or phrase, by any method. It may consist of a group of letters or words taken from the full version of the word or phrase; for example, the word ''abbrevia ...
of "eXternal Subroutine", where ''external'' refers to
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s external to Perl. XS also refers to a
glue language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
for specifying calling interfaces supporting such interfaces (see below).


Background

Subroutine libraries in Perl are called ''modules'', and modules that contain xsubs are called ''XS modules''. Perl provides a framework for developing, packaging, distributing, and installing modules. It may be desirable for a Perl program to invoke a C subroutine in order to handle very CPU or
memory Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembered, ...
intensive tasks, to interface with hardware or low-level system facilities, or to make use of existing C subroutine libraries.


Perl interpreter

The Perl interpreter is a C program, so in principle there is no obstacle to calling from Perl to C. However, the XS interface is complex and highly technical, and using it requires some understanding of the interpreter. The earliest reference on the subject was th
perlguts
POD.


Wrappers

It is possible to write XS modules that
wrap Wrap, WRAP or Wrapped may refer to: Storage and preservation * Gift wrap or wrap paper, used to enclose a present * Overwrap, a wrapping of items in a package or a wrapping over packages * Plastic wrap, a thin, clear, flexible plastic used to co ...
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
code. Doing so is mostly a matter of configuring the module build system.


Example code

The following shows an XS module that exposes a function concat() to concatenate two strings (i.e., the equivalent of Perl’s . operator). #define PERL_NO_GET_CONTEXT #include "EXTERN.h" #include "perl.h" #include "XSUB.h" SV* _do_sv_catsv (pTHX_ SV* one_sv, SV* two_sv ) MODULE = Demo::XSModule PACKAGE = Demo::XSModule SV* concat (SV* one_sv, SV* two_sv) CODE: SV* to_return = _do_sv_catsv( aTHX_ one_sv, two_sv ); RETVAL = to_return; OUTPUT: RETVAL The first four lines (#define and #include statements) are standard boilerplate. After then follow any number of plain C functions that are callable locally. The section that starts with MODULE = Demo::XSModule defines the Perl interface to this code using the actual XS macro language. Note that the C code under the CODE: section calls the _do_sv_catsv() pure-C function that was defined in the prior section. Perl’s documentation explains the meaning and purpose of all of the “special” symbols (e.g., aTHX_ and RETVAL) shown above. To make this module available to Perl it must be compiled. Build tools lik
ExtUtils::MakeMaker
can do this automatically. (To build manually: th
xsubpp
tool parses an XS module and outputs C source code; that source code is then compiled to a shared library and placed in a directory where Perl can find it.) Perl code then uses a module lik
XSLoader
to load the compiled XS module. At this point Perl can call Demo::XSModule::concat('foo', 'bar') and receive back a string foobar, as if concat() were itself written in Perl. Note that, for building Perl interfaces to preexisting C libraries, the h2xs can automate much of the creation of the XS file itself.


Difficulties

Creation and maintenance of XS modules requires expertise with C itself as well as Perl’s extensive C API. XS modules may only be installed if a
C compiler This page is intended to list all current compilers, compiler generators, interpreters, translators, tool foundations, assemblers, automatable command line interfaces ( shells), etc. Ada Compilers ALGOL 60 compilers ALGOL 68 compilers cf. ...
and the
header file Many programming languages and other computer files have a directive, often called include (sometimes copy or import), that causes the contents of the specified file to be inserted into the original file. These included files are called copybooks ...
s that the Perl interpreter was compiled against are available. Also, new versions of Perl may break
binary compatibility Binary-code compatibility (binary compatible or object-code-compatible) is a property of a computer system, meaning that it can run the same executable code, typically machine code for a general-purpose computer CPU, that another computer syst ...
requiring XS modules to be recompiled.


See also

*
SWIG The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other language ...
, an alternative to XS which also supports calling C and C++ functions from several other languages. * FFI, a mechanism which enables calling routines written in another language.


References

* Jenness, Tim & Cozens, Simon (2002). "Extending and Embedding Perl". Greenwich: Manning Publications Co.


External links


perlxs
Perl XS application programming interface

Perl XS tutorial

Perl internal functions for those doing extensions

Perl API listing (autogenerated)

tutorial

building XS modules for C++
xs-fun
XS is fun: a simple and easy tutorial on writing Perl XS {{Perl Perl