XS (Perl)

XS is a Perl foreign function interface through which a program can call a C or C++ subroutine. XS or xsub is an abbreviation of "eXternal Subroutine", where external refers to programming languages external to Perl.

XS also refers to a glue language 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 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 the perlguts POD.

Wrappers

It is possible to write XS modules that wrap C++ code. Doing so is mostly a matter of configuring the module build system.[1]

XS module creation

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. 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 module. Once the module is loaded, the C subroutines can be called from Perl code, as if they were written in Perl.

Difficulties

XS modules are difficult to write and maintain, and they may only be installed if a C compiler and the header files that the Perl interpreter was compiled against are available. Also, new versions of Perl may break binary compatibility requiring XS modules to be recompiled.

gollark: - I believe we should end racial discrimination by replacing computer monitors with 1-bit black and white displays so race cannot be distinguished.
gollark: - As eating meat places suffering on millions of innocent animals, I believe animal meat should be replaced with human flesh from donors, as humans are able to meaningfully consent to this while animals are not (and don't get a choice in practice anyway).
gollark: - To increase the efficiency of the education system and encourage self-directed learning, I believe schools should lock children in individual cubicles with textbooks for 5 hours a day instead of using classrooms and teachers.
gollark: - It's important to me that women aren't forced to have children they don't want or may not be able to take care of.- which is why I support mandatory sterilization for all - children would be grown in vats and raised by the government instead.
gollark: - I support the right to privacy!- In light of governments' large-scale mass surveillance campaigns which they do not seem inclined to stop, I would support an open and transparent volunteer spying agency using open source software and hardware to gather and process data in order to act as a competitor.

See also

  • SWIG, an alternative to XS which also supports calling C and C++ functions from several other languages.

References

  1. "Gluing C++ And Perl Together". johnkeiser.com. August 27, 2001.
  • Jenness, Tim & Cozens, Simon (2002). "Extending and Embedding Perl". Greenwich: Manning Publications Co. ISBN 1-930110-82-0
  • 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++
  • xs-fun XS is fun: a simple and easy tutorial on writing Perl XS
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.