XS (Perl)
Encyclopedia
XS is an interface through which a Perl
program can call a C
or C++
language subroutine
. The letters stand for eXternal Subroutine, or xsub, where external means external to Perl, i.e. written in some other language.
It may be desirable for a Perl program to invoke a C subroutine in order to handle very CPU or memory
intensive tasks, to interface with hardware
or low-level system facilities, or to make use of existing C subroutine libraries.
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 using it requires some understanding of the interpreter. The earliest reference on the subject was the perlguts POD
, the word guts suggesting the highly technical nature of the material.
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. To create an XS module,
Once an XS module is installed, it can be loaded into a Perl program with a
. Once the module is loaded, the C subroutines can be called from Perl code, as if they were written in Perl.
XS modules are not without drawbacks. They are difficult to write and maintain, and they may only be installed if a C compiler and the headers that the Perl interpreter were compiled against are available. Also, new versions of Perl may break binary compatibility; if this happens, all XS modules must be recompiled.
It is also possible to write XS modules that wrap C++
code. Doing so is mostly a matter of wrestling with the module build system. Details on how to do this are given here.
, an alternative to XS which also supports calling C and C++ functions from several other languages.
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
program can call a C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
or C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
language subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
. The letters stand for eXternal Subroutine, or xsub, where external means external to Perl, i.e. written in some other language.
It may be desirable for a Perl program to invoke a C subroutine in order to handle very CPU or memory
Ram
-Animals:*Ram, an uncastrated male sheep*Ram cichlid, a species of freshwater fish endemic to Colombia and Venezuela-Military:*Battering ram*Ramming, a military tactic in which one vehicle runs into another...
intensive tasks, to interface with hardware
Computer hardware
Personal computer hardware are component devices which are typically installed into or peripheral to a computer case to create a personal computer upon which system software is installed including a firmware interface such as a BIOS and an operating system which supports application software that...
or low-level system facilities, or to make use of existing C subroutine libraries.
The Perl interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...
is a C program, so in principle there is no obstacle to calling from Perl to C. However, the XS interface is complex, and using it requires some understanding of the interpreter. The earliest reference on the subject was the perlguts POD
Plain Old Documentation
Plain Old Documentation, abbreviated pod, is a lightweight markup language used to document the Perl programming language.-Design:pod is designed to be a simple, clean language with just enough syntax to be useful. It purposefully does not include mechanisms for fonts, images, colors or tables...
, the word guts suggesting the highly technical nature of the material.
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. To create an XS module,
h2xs
is first used to create an empty module. Next, C subroutines are added, and the calling interfaces to the C subroutines are specified with a specialized glue language (also referred to as XS). Finally, the module is compiled, tested, and installed with make
.Once an XS module is installed, it can be loaded into a Perl program with a
use
statement, like an ordinary Perl modulePerl module
A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl's package mechanism that has become universally adopted....
. Once the module is loaded, the C subroutines can be called from Perl code, as if they were written in Perl.
XS modules are not without drawbacks. They are difficult to write and maintain, and they may only be installed if a C compiler and the headers that the Perl interpreter were compiled against are available. Also, new versions of Perl may break binary compatibility; if this happens, all XS modules must be recompiled.
It is also possible to write XS modules that wrap C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
code. Doing so is mostly a matter of wrestling with the module build system. Details on how to do this are given here.
See also
SWIGSWIG
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 languages like C#, Java, Modula-3, Objective Caml, Octave, and Scheme...
, an alternative to XS which also supports calling C and C++ functions from several other languages.
External links
- perlxs Perl XS application programming interface
- perlxstut Perl XS tutorial
- perlguts Perl internal functions for those doing extensions
- perlapi Perl API listing (autogenerated)
- XS Mechanics tutorial
- Perl and C++ building XS modules for C++