可以使用Inine模块轻松实现,掌握该技术需要阅读的文档如下:
About Inline:
0. Inline
http://search.cpan.org/~ingy/Inline-0.44/Inline.pod
1. Inline::C
http://search.cpan.org/~ingy/Inline-0.44/C/C.pod
2. Inline::C-Cookbook (使用Inline::C的实例)
http://www.penguin-soft.com/penguin/man/3/Inline::C-Cookbook.html
3. Inline::CPP
http://search.cpan.org/~neilw/Inline-CPP-0.25/lib/Inline/CPP.pod
第一部分
Perl的Inline模块
Write Perl subroutines in other programming languages
http://search.cpan.org/~ingy/Inline-0.44/Inline.pod
<!--[if supportFields]><span lang=EN-US style='font-size:12.0pt'><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'></span>TOC \o "1-3" \h \z <span style='mso-element:field-separator'></span></span><![endif]-->Perl的Inline模块... <!--[if supportFields]><span style='color:windowtext;display:none;mso-hide:screen;text-decoration:none; text-underline:none'><span style='mso-element:field-begin'></span></span><span style='color:windowtext;display:none;mso-hide:screen;text-decoration:none; text-underline:none'> PAGEREF _Toc181012984 \h </span><span style='color:windowtext; display:none;mso-hide:screen;text-decoration:none;text-underline:none'><span style='mso-element:field-separator'></span></span><![endif]-->1<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380034000000</w:data> </xml><![endif]--><!--[if supportFields]><span style='color:windowtext; display:none;mso-hide:screen;text-decoration:none;text-underline:none'><span style='mso-element:field-end'></span></span><![endif]-->
<!--[if supportFields]><span lang=EN-US style='font-size:12.0pt'><span style='mso-element:field-end'></span></span><![endif]-->
<!--[if !supportLists]-->1. <!--[endif]-->Name:
Inline - Write Perl subroutines in other programming languages.
Supporting (C, C++, Java, Python, Ruby, Tcl, Assembler, Basic, Guile, Befunge, Octave, <place w:st="on"><city w:st="on">Awk</city>, <state w:st="on">BC</state></place>, TT (Template Toolkit), WebChat and even PERL)
<!--[if !supportLists]-->2. <!--[endif]-->An Example:
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;
}
<!--[if !supportLists]-->3. <!--[endif]-->DESCRIPTION:
1) The Inline module allows you to put source code from other programming languages directly "inline" in a Perl script or module. The code is automatically compiled as needed, and then loaded for immediate access from Perl.
Simply type the code where you want it and run your Perl as normal.
2) XS or SWIG
3) 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.
4) Why Inline?
Why would I use other languages in Perl?
a. Performance
b. Access functionality from existing API-s that use the language. Some of this code may only be available in binary form.
Why should I use Inline to do it?
<!--[if !supportLists]-->a. <!--[endif]-->Already two major facilities for extending Perl with C: XS and SWIG (quite difficult to learn compared to Inline)
<!--[if !supportLists]-->b. <!--[endif]-->C Cookbook with lots of short but complete programs that you can extend to your real-life problems
<!--[if !supportLists]-->c. <!--[endif]-->You can use it directly in a script. With XS and SWIG, you always set up an entirely separate module.
<!--[if !supportLists]-->d. <!--[endif]-->Inline supports several programming languages: C, C++, Java, …
<!--[if !supportLists]-->4. <!--[endif]-->Using the Inline.pm Module
1) Inline is a little bit different than most of the Perl modules that you are used to.
Doesn't import any functions into your namespace and it doesn't have any object oriented methods.
Its entire interface (with two minor exceptions) is specified through the 'use Inline ...'
command.
2) Basics: Using Inline
use Inline X => "X source code";
‘X’: the supported Inline programming languages(C, C++, Java, …)
‘X source code’: identifies the source code that you want to bind to Perl.
The source code can be specified using any of the following syntaxes:
<!--[if !supportLists]-->a. <!--[endif]-->DATA Keyword (recommend)
<!--[if !supportLists]-->b. <!--[endif]-->FILE and BELOW keywords (more for testing)
<!--[if !supportLists]-->c. <!--[endif]-->Strings (Perl's "here document" style of quoting: <<’END’ … END | BEGIN…)
<!--[if !supportLists]-->d. <!--[endif]-->bind() Function (The bind()
method takes the same arguments as 'use Inline ...'
.)
Notes: can omit keyword altogether.
确定下来的使用的方式:
use strict;
use Inline X=> ‘DATA’;
# Perl code goes here …
__DATA__
__X__
/* X code goes here …*/
3) More about the DATA Section (没看懂…)
4) Configuration Options (没看懂…) (On and Off; with; )
5) command line (shortcuts) for debugging (-MInline=INFO sth.pl …)
6) Inline Directory:
a. Inline needs a place to build your code and to install the results of the build.
“_Inline/” directory
b. understanding this directory: 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.
c. Debugging Inline Errors: 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.
d. The 'config' Registry File
<!--[if !supportLists]-->5. <!--[endif]-->Configuration Options
1) DIRECTORY
2) NAME
3) …
<!--[if !supportLists]-->6. <!--[endif]-->Inline Configuration Shortcuts
Specify configuration options from the command line
Configuration shortcuts: …
<!--[if !supportLists]-->7. <!--[endif]-->Writing Modules with Inline
<!--[if !supportLists]-->8. <!--[endif]-->How Inline Works …
<!--[if !supportLists]-->9. <!--[endif]-->See Also
1) information about using inline with C: Inline::C
http://search.cpan.org/~ingy/Inline-0.44/C/C.pod
Sample programs using inline with C: Inline::C-Cookbook
http://www.penguin-soft.com/penguin/man/3/Inline::C-Cookbook.html
2)
Inline-FAQ: http://search.cpan.org/~ingy/Inline-0.44/Inline-FAQ.pod
How do I create a binary distribution using Inline?
Inline-Support: http://search.cpan.org/~ingy/Inline-0.44/Inline-Support.pod
For information on writing your own Inline Language Support Module, see Inline-API:
http://search.cpan.org/~ingy/Inline-0.44/Inline-API.pod
3) other inlineS: Inline::CPP, Inline::Python, Inline::Java, Inline::Ruby, Inline::ASM …