Perl中使用其他语言,使用Inline模块(一):Inline

实现在Perl中使用其他语言编写的代码,如C, C++, Java, Ruby, ASM, Python…….
可以使用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


第一部分

PerlInline模块

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 &quot;1-3&quot; \h \z <span style='mso-element:field-separator'></span></span><![endif]-->PerlInline模块... <!--[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]-->

1. Name: <!--[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 _Toc181012985 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380035000000</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]-->

2. An Example: <!--[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 _Toc181012986 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380036000000</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]-->

3. DESCRIPTION: <!--[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 _Toc181012987 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380037000000</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]-->

4. Using the Inline.pm Module. <!--[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 _Toc181012988 \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]-->2<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380038000000</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]-->

5. Configuration Options <!--[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 _Toc181012989 \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]-->3<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900380039000000</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]-->

6. Inline Configuration Shortcuts <!--[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 _Toc181012990 \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]-->3<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900390030000000</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]-->

7. Writing Modules with 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 _Toc181012991 \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]-->3<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900390031000000</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]-->

8. How Inline Works …... <!--[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 _Toc181012992 \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]-->3<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900390032000000</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]-->

9. See Also. <!--[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 _Toc181012993 \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]-->3<!--[if gte mso 9]><xml> <w:data>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310032003900390033000000</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 …

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值