设为首页 收藏本站
查看: 560|回复: 0

[经验分享] Perl中使用其他语言,使用Inline模块(一):Inline

[复制链接]

尚未签到

发表于 2017-5-19 11:21:39 | 显示全部楼层 |阅读模式
实现在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



第一部分


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 &quot;1-3&quot; \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]-->
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379054-1-1.html 上篇帖子: Perl 之LWP 与 WEB 的基本使用(zz) 下篇帖子: Perl lib version (v5.8.3) doesn't match executable version
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表