- Perl ›
- here
Getting Started with Perl XS - C Binding to Perl
I will explain the binding of C language to Perl using XS. First of all, I will explain how to create an XS module.
If you don't understand C language at all, we offer the following courses.
1. Creating an XS module
Let's actually create an XS module. The XS module creation command, Makefile.PL, and make are explained carefully.
2. perlapi
Explains how to manipulate Perl data from XS. Perl is implemented in C, but there are many APIs for calling Perl functions from C.
3. Makefile.PL related
To generate a Makefile, Perl creates a file called Makefile.PL and uses a module called ExtUtil::MakeMaker. I will post an article to use this module called ExtUtil::MakeMaker conveniently.
4. Type conversion
Using XS to use the C language from Perl means that you need to freely convert between Perl types and C language types. I will explain how to convert the type.
5. XS Technique
Introducing the XS technique.
Use multiple modules
For example, if you want to implement two modules, MyModule and MyModule::Util, on XS, here is an easy way to do it.
Memory management
Since XS will be written in C language, memory management must be done manually. In XS, Perl data is set to a mortal state, and there is a mechanism to automatically release it, which is convenient.
6. Cooperation with C language
I will explain how to link C language libraries and C language structures with XS.
How to load a C language library
Please refer to the following article for how to use the library created in C language from XS.
Handling of C data types
I will explain how to use C language structures as Perl data. If you want to make a C language structure correspond to a Perl object, refer to the following article.
7. Cooperation with C ++
If you want to use C ++ in XS, please refer to the following article. Explains how to write XS file and Makefile.PL when writing C ++.
In C ++, there are classes in addition to C structs. The method of handling C ++ classes in XS is explained below.
Official XS documentation
A link to the official Perl XS documentation.
Other useful features include perlapi for manipulating Perl data and perlguts, which describes Perl's internal implementation.