InlineSection: User Contributed Perl Documentation (3)Updated: 2002-10-28 |
InlineSection: User Contributed Perl Documentation (3)Updated: 2002-10-28 |
use Inline C;
print "9 + 16 = ", add(9, 16), "\n";
print "9 - 16 = ", subtract(9, 16), "\n";
__END__
__C__
int add(int x, int y) {
return x + y;
}
int subtract(int x, int y) {
return x - y;
}
Inline saves you from the hassle of having to write and compile your own glue code using facilities like XS or SWIG. Simply type the code where you want it and run your Perl as normal. All the hairy details are handled for you. The compilation and installation of your code chunks all happen transparently; all you will notice is the delay of compilation on the first run.
The Inline code only gets compiled the first time you run it (or whenever it is modified) so you only take the performance hit once. Code that is Inlined into distributed modules (like on the CPAN) will get compiled when the module is installed, so the end user will never notice the compilation time.
Best of all, it works the same on both Unix and Microsoft Windows. See Inline-Support for support information.
Another reason is to access functionality from existing API-s that use the language. Some of this code may only be available in binary form. But by creating small subroutines in the native language, you can ``glue'' existing libraries to your Perl. As a user of the CPAN, you know that code reuse is a good thing. So why throw away those Fortran libraries just yet?
If you are using Inline with the C language, then you can access the full internals of Perl itself. This opens up the floodgates to both extreme power and peril.
Maybe the best reason is ``Because you want to!''. Diversity keeps the world interesting. TMTOWTDI!
There is a big fat learning curve involved with setting up and using the XS environment. You need to get quite intimate with the following docs:
* perlxs * perlxstut * perlapi * perlguts * perlmod * h2xs * xsubpp * ExtUtils::MakeMaker
With Inline you can be up and running in minutes. There is a C Cookbook with lots of short but complete programs that you can extend to your real-life problems. No need to learn about the complicated build process going on in the background. You don't even need to compile the code yourself. Inline takes care of every last detail except writing the C code.
Perl programmers cannot be bothered with silly things like compiling. ``Tweak, Run, Tweak, Run'' is our way of life. Inline does all the dirty work for you.
Another advantage of Inline is that you can use it directly in a script. You can even use it in a Perl one-liner. With XS and SWIG, you always set up an entirely separate module. Even if you only have one or two functions. Inline makes easy things easy, and hard things possible. Just like Perl.
Finally, Inline supports several programming languages (not just C and C++). As of this writing, Inline has support for C, C++, Java, Python, Ruby, Tcl, Assembler, Basic, Guile, Befunge, Octave, Awk, BC, TT (Template Toolkit), WebChat and even PERL. New Inline Language Support Modules (ILSMs) are regularly being added. See Inline-API for details on how to create your own ILSM.
This section will explain all of the different ways to "use Inline". If you want to begin using C with Inline immediately, see Inline::C-Cookbook.
use Inline X => "X source code";
where 'X' is one of the supported Inline programming languages. The second parameter identifies the source code that you want to bind to Perl. The source code can be specified using any of the following syntaxes:
use Inline Java => 'DATA';
# Perl code goes here ...
__DATA__
__Java__
/* Java code goes here ... */
The easiest and most visually clean way to specify your source code in an Inline Perl program is to use the special "DATA" keyword. This tells Inline to look for a special marker in your "DATA" filehandle's input stream. In this example the special marker is "__Java__", which is the programming language surrounded by double underscores.
In case you've forgotten, the "DATA" pseudo file is comprised of all the text after the "__END__" or "__DATA__" section of your program. If you're working outside the "main" package, you'd best use the "__DATA__" marker or else Inline will not find your code.
Using this scheme keeps your Perl code at the top, and all the ugly Java stuff down below where it belongs. This is visually clean and makes for more maintainable code. An excellent side benefit is that you don't have to escape any characters like you might in a Perl string. The source code is verbatim. For these reasons, I prefer this method the most.
The only problem with this style is that since Perl can't read the "DATA" filehandle until runtime, it obviously can't bind your functions until runtime. The net effect of this is that you can't use your Inline functions as barewords (without predeclaring them) because Perl has no idea they exist during compile time.
use Inline::Files;
use Inline Java => 'FILE';
# Perl code goes here ...
__JAVA__
/* Java code goes here ... */
This is the newest method of specifying your source code. It makes use of the Perl module "Inline::Files" written by Damian Conway. The basic style and meaning are the same as for the "DATA" keyword, but there are a few syntactic and semantic twists.
First, you must say 'use Inline::Files' before you 'use Inline' code that needs those files. The special '"DATA"' keyword is replaced by either '"FILE"' or '"BELOW"'. This allows for the bad pun idiom of:
use Inline C => 'BELOW';
You can omit the "__DATA__" tag now. Inline::Files is a source filter that will remove these sections from your program before Perl compiles it. They are then available for Inline to make use of. And since this can all be done at compile time, you don't have to worry about the caveats of the 'DATA' keyword.
This module has a couple small gotchas. Since Inline::Files only recognizes file markers with capital letters, you must specify the capital form of your language name. Also, there is a startup time penalty for using a source code filter.
At this point Inline::Files is alpha software and use of it is experimental. Inline's integration of this module is also fledgling at the time being. One of things I plan to do with Inline::Files is to get line number info so when an extension doesn't compile, the error messages will point to the correct source file and line number.
My best advice is to use Inline::Files for testing (especially as support for it improves), but use DATA for production and distributed/CPAN code.
use Inline Java => <<'END';
/* Java code goes here ... */
END
# Perl code goes here ...
You also just specify the source code as a single string. A handy way to write the string is to use Perl's ``here document'' style of quoting. This is ok for small functions but can get unwieldy in the large. On the other hand, the string variant probably has the least startup penalty and all functions are bound at compile time.
If you wish to put the string into a scalar variable, please be aware that the "use" statement is a compile time directive. As such, all the variables it uses must also be set at compile time, "before" the 'use Inline' statement. Here is one way to do it:
my $code;
BEGIN {
$code = <<END;
/* Java code goes here ... */
END
}
use Inline Java => $code;
# Perl code goes here ...
my $code = <<END;
/* Java code goes here ... */
END
Inline->bind(Java => $code);
You can think of "bind()" as a way to "eval()" code in other programming languages.
Although bind() is a powerful feature, it is not recommended for use in Inline based modules. In fact, it won't work at all for installable modules. See instructions below for creating modules with Inline.
use Inline 'Java';
# Perl code goes here ...
__DATA__
__Java__
/* Java code goes here ... */
or
use Inline::Files;
use Inline 'Java';
# Perl code goes here ...
__JAVA__
/* Java code goes here ... */
You can also specify multiple Inline sections, possibly in different programming languages. Here is another example:
# The module Foo.pm
package Foo;
use AutoLoader;
use Inline C;
use Inline C => DATA => FILTERS => 'Strip_POD';
use Inline Python;
1;
__DATA__
sub marine {
# This is an autoloaded subroutine
}
=head1 External subroutines
=cut
__C__
/* First C section */
__C__
/* Second C section */
=head1 My C Function
Some POD doc.
=cut
__Python__
"""A Python Section"""
An important thing to remember is that you need to have one ``use Inline Foo => 'DATA''' for each ``__Foo__'' marker, and they must be in the same order. This allows you to apply different configuration options to each section.
use Inline (C => 'DATA',
DIRECTORY => './inline_dir',
LIBS => '-lfoo',
INC => '-I/foo/include',
PREFIX => 'XXX_',
WARNINGS => 0,
);
You can also specify the configuration options on a separate Inline call like this:
use Inline (C => Config =>
DIRECTORY => './inline_dir',
LIBS => '-lfoo',
INC => '-I/foo/include',
PREFIX => 'XXX_',
WARNINGS => 0,
);
use Inline C => <<'END_OF_C_CODE';
The special keyword 'Config' tells Inline that this is a configuration-only call. No source code will be compiled or bound to Perl.
If you want to specify global configuration options that don't apply to a particular language, just leave the language out of the call. Like this:
use Inline Config => WARNINGS => 0;
The Config options are inherited and additive. You can use as many Config calls as you want. And you can apply different options to different code sections. When a source code section is passed in, Inline will apply whichever options have been specified up to that point. Here is a complex configuration example:
use Inline (Config =>
DIRECTORY => './inline_dir',
);
use Inline (C => Config =>
LIBS => '-lglobal',
);
use Inline (C => 'DATA', # First C Section
LIBS => ['-llocal1', '-llocal2'],
);
use Inline (Config =>
WARNINGS => 0,
);
use Inline (Python => 'DATA', # First Python Section
LIBS => '-lmypython1',
);
use Inline (C => 'DATA', # Second C Section
LIBS => [undef, '-llocal3'],
);
The first "Config" applies to all subsequent calls. The second "Config" applies to all subsequent "C" sections (but not "Python" sections). In the first "C" section, the external libraries "global", "local1" and "local2" are used. (Most options allow either string or array ref forms, and do the right thing.) The "Python" section does not use the "global" library, but does use the same "DIRECTORY", and has warnings turned off. The second "C" section only uses the "local3" library. That's because a value of "undef" resets the additive behavior.
The "DIRECTORY" and "WARNINGS" options are generic Inline options. All other options are language specific. To find out what the "C" options do, see "Inline::C".
use Inline Config =>
FORCE_BUILD => 1,
CLEAN_AFTER_BUILD => 0;
could be reworded as:
use Inline Config =>
ENABLE => FORCE_BUILD,
DISABLE => CLEAN_AFTER_BUILD;
use Inline with => 'Event';
This tells Inline to load the module "Event.pm" and ask it for configuration information. Since "Event" has a C API of its own, it can pass Inline all of the information it needs to be able to use "Event" C callbacks seamlessly.
That means that you don't need to specify the typemaps, shared libraries, include files and other information required to get this to work.
You can specify a single module or a list of them. Like:
use Inline with => qw(Event Foo Bar);
Currently, "Event" is the only module that works with Inline.
For instance, to get some general information about your Inline code in the script "Foo.pl", use the command:
perl -MInline=INFO Foo.pl
If you want to force your code to compile, even if its already done, use:
perl -MInline=FORCE Foo.pl
If you want to do both, use:
perl -MInline=INFO -MInline=FORCE Foo.pl
or better yet:
perl -MInline=INFO,FORCE Foo.pl
One of the key factors to using Inline successfully, is understanding this directory. When developing code it is usually best to create this directory (or let Inline do it) in your current directory. Remember that there is nothing sacred about this directory except that it holds your compiled code. Feel free to delete it at any time. Inline will simply start from scratch and recompile your code on the next run. If you have several programs that you want to force to recompile, just delete your '.Inline/' directory.
It is probably best to have a separate '.Inline/' directory for each project that you are working on. You may want to keep stable code in the <.Inline/> in your home directory. On multi-user systems, each user should have their own '.Inline/' directories. It could be a security risk to put the directory in a shared place like "/tmp/".
When Inline needs to build something it creates a subdirectory under your "DIRECTORY/build/" directory. This is where it writes all the components it needs to build your extension. Things like XS files, Makefiles and output log files.
If everything goes OK, Inline will delete this subdirectory. If there is an error, Inline will leave the directory intact and print its location. The idea is that you are supposed to go into that directory and figure out what happened.
Read the doc for your particular Inline Language Support Module for more information.
Whenever you add a new ILSM, you should delete this file so that Inline will auto-discover your newly installed language module.
Normally Inline will search in a bunch of known places for a directory called '.Inline/'. Failing that, it will create a directory called '_Inline/'
If you want to specify your own directory, use this configuration option.
Note that you must create the "DIRECTORY" directory yourself. Inline will not do it for you.
use Inline C => 'DATA',
NAME => 'Foo::Bar';
would cause your C code to be compiled in to the object:
lib/auto/Foo/Bar/Bar.so
lib/auto/Foo/Bar/Bar.inl
(The .inl component contains dependency information to make sure the source code is in sync with the executable)
If you don't use NAME, Inline will pick a name for you based on your program name or package name. In this case, Inline will also enable the AUTONAME option which mangles in a small piece of the MD5 fingerprint into your object name, to make it unique.
use Inline C => 'DATA',
DISABLE => 'AUTONAME';
AUTONAME mangles in enough of the MD5 fingerprint to make your module name unique. Objects created with AUTONAME will never get replaced. That also means they will never get cleaned up automatically.
AUTONAME is very useful for small throw away scripts. For more serious things, always use the NAME option.
The presence of the VERSION parameter is the official way to let Inline know that your code is an installable/installed module. Inline will never generate an object in the temporary cache (_Inline/ directory) if VERSION is set. It will also never try to recompile a module that was installed into someone's Perl site tree.
So the basic rule is develop without VERSION, and deliver with VERSION.
use Event;
use Inline C => DATA => WITH => 'Event';
Modules specified using the config form of "WITH" will not be automatically required. You must "use" them yourself.
There is a slight startup penalty by using SAFEMODE. Also, using UNTAINT automatically turns this option on. If you need your code to start faster under "-T" (taint) checking, you'll need to turn this option off manually. Only do this if you are not worried about security risks. See the "UNSAFE" shortcut below.
perl -MInline=NOCLEAN inline_script.pl
or
perl -MInline=Info,force,NoClean inline_script.pl
You can specify multiple shortcuts separated by commas. They are not case sensitive. You can also specify shorcuts inside the Inline program like this:
use Inline 'Info', 'Force', 'Noclean';
NOTE: If a 'use Inline' statement is used to set shortcuts, it can not be used for additional purposes.
NOTE: REPORTBUG informs you to use the tar command. If your system does not have tar, please use the equivalent "zip" command.
h2xs -PAXn Math::Simple
This will generate a bunch of files that form a skeleton of what you need for a distributable module. (Read the h2xs manpage to find out what the options do) Next, modify the "Simple.pm" file to look like this:
package Math::Simple;
$VERSION = '1.23';
use base 'Exporter';
@EXPORT_OK = qw(add subtract);
use strict;
use Inline C => 'DATA',
VERSION => '1.23',
NAME => 'Math::Simple';
1;
__DATA__
=pod
=cut
__C__
int add(int x, int y) {
return x + y;
}
int subtract(int x, int y) {
return x - y;
}
The important things to note here are that you must specify a "NAME" and "VERSION" parameter. The "NAME" must match your module's package name. The "VERSION" parameter must match your module's $VERSION variable and they must be of the form "/^\d\.\d\d$/".
NOTE: These are Inline's sanity checks to make sure you know what you're doing before uploading your code to CPAN. They insure that once the module has been installed on someone's system, the module would not get automatically recompiled for any reason. This makes Inline based modules work in exactly the same manner as XS based ones.
Finally, you need to modify the Makefile.PL. Simply change:
use ExtUtils::MakeMaker;
to
use Inline::MakeMaker;
When the person installing "Math::Simple" does a ""make"", the generated Makefile will invoke Inline in such a way that the C code will be compiled and the executable code will be placed into the "./blib" directory. Then when a ""make install"" is done, the module will be copied into the appropiate Perl sitelib directory (which is where an installed module should go).
Now all you need to do is:
perl Makefile.PL
make dist
That will generate the file "Math-Simple-0.20.tar.gz" which is a distributable package. That's all there is to it.
IMPORTANT NOTE: Although the above steps will produce a workable module, you still have a few more responsibilities as a budding new CPAN author. You need to write lots of documentation and write lots of tests. Take a look at some of the better CPAN modules for ideas on creating a killer test harness. Actually, don't listen to me, go read these:
perldoc perlnewmod
http://www.cpan.org/modules/04pause.html
http://www.cpan.org/modules/00modlist.long.html
Inline performs the following steps:
use Inline C => "Source-Code";
or
use Inline;
bind Inline C => "Source-Code";
where "C" is the programming language of the source code, and "Source-Code" is a string, a file name, an array reference, or the special 'DATA' keyword.
Since Inline is coded in a ""use"" statement, everything is done during Perl's compile time. If anything needs to be done that will affect the "Source-Code", it needs to be done in a "BEGIN" block that is before the ""use Inline ..."" statement. If you really need to specify code to Inline at runtime, you can use the "bind()" method.
Source code that is stowed in the 'DATA' section of your code, is read in by an "INIT" subroutine in Inline. That's because the "DATA" filehandle is not available at compile time.
example_pl_3a9a.so
example_pl_3a9a.inl
If all the contingency information matches the values stored in the ".inl" file, then proceed to step 8. (No compilation is necessary)
By default Inline will try to build and install under the first place that meets one of the following conditions:
A) The DIRECTORY= config option; if specified
B) The PERL_INLINE_DIRECTORY environment variable; if set
C) .Inline/ (in current directory); if exists and $PWD != $HOME
D) bin/.Inline/ (in directory of your script); if exists
E) ~/.Inline/; if exists
F) ./_Inline/; if exists
G) bin/_Inline; if exists
H) Create ./_Inline/; if possible
I) Create bin/_Inline/; if possible
Failing that, Inline will croak. This is rare and easily remedied by just making a directory that Inline will use;
If the module option is being compiled for permanent installation, then Inline will only use "./_Inline/" to build in, and the $Config{installsitearch} directory to install the executable in. This action is caused by Inline::MakeMaker, and is intended to be used in modules that are to be distributed on the CPAN, so that they get installed in the proper place.
Other languages like Python and Java, provide their own loaders.
For sample programs using Inline with C see Inline::C-Cookbook.
For ``Formerly Answered Questions'' about Inline, see Inline-FAQ.
For information on supported languages and platforms see Inline-Support.
For information on writing your own Inline Language Support Module, see Inline-API.
Inline's mailing list is inline@perl.org
To subscribe, send email to inline-subscribe@perl.org
- Put "use Inline REPORTBUG;" at the top of your code, or use the command line option "perl -MInline=REPORTBUG ...". - Run your code. - Follow the printed directions.
Neil Watkiss <NEILW@cpan.org> is the author of "Inline::CPP", "Inline::Python", "Inline::Ruby", "Inline::ASM", "Inline::Struct" and "Inline::Filters". He is known in the innermost Inline circles as the ``Boy Wonder''.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html