Perl中使用其他语言,使用Inline模块(二):Inline::C和大量实例Inline::C-Cookbook

本文介绍了一个Perl模块Inline::C,它允许开发者在Perl中直接编写C语言函数,并提供了详细的使用指南,包括函数定义、配置选项及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Inline::C

<!--[if supportFields]><span lang=EN-US><span style='mso-element:field-begin'></span><span style='mso-spacerun:yes'></span>TOC \o &quot;1-3&quot; \h \z \u <span style='mso-element:field-separator'></span></span><![endif]-->Inline::C. <!--[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 _Toc181016685 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600380035000000</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 _Toc181016686 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600380036000000</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 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 _Toc181016687 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600380037000000</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 Usage. <!--[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 _Toc181016688 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600380038000000</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 Function Definitions <!--[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 _Toc181016689 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600380039000000</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 C 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 _Toc181016690 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600390030000000</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 C-Perl Bindings and The Inline Stack Macros <!--[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 _Toc181016691 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600390031000000</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 C Subroutines <!--[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 _Toc181016692 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600390032000000</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 Examples <!--[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 _Toc181016693 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600390033000000</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 Inline::C-Cookbook (More Inline::C Examples) <!--[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 _Toc181016694 \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>08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F0054006F0063003100380031003000310036003600390034000000</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><span style='mso-element:field-end'></span></span><![endif]-->

<!--[if !supportLists]-->1 <!--[endif]-->Name

Inline::C - Write Perl Subroutines in C

<!--[if !supportLists]-->2 <!--[endif]-->Description

Inline::C is a module that allows you to write Perl subroutines in C.

Since version 0.30 the Inline module supports multiple programming languages and each language has its own support module. This document describes how to use Inline with the C programming language.

<!--[if !supportLists]-->3 <!--[endif]-->Usage

1) It is just a support module for using Inline.pm with C. So the usage is always:

use Inline C => ...;

or

bind Inline C => ...;
2) An Example:
use strict;

use Inline C=> ‘DATA’;

# perl code goes here …

__DATA__

__C__

/*C code goes here …*/

<!--[if !supportLists]-->4 <!--[endif]-->Function Definitions

1) Function definition in C code which will be bound to Perl:

return-type function-name (type-name-pairs) {/*function body*/…}

2) type: int, long, double, char*, SV* (pointer to Scalar Value)(char*是字符串,SV*是数字串)

return-type: void, and Ditto

3) Notice:

Inline only looks for function definitions, not function prototypes. Definitions are the syntax directly preceeding a function body.

Inline does not scan external files, like headers. (Although other libraries can linked in, and called from C-space, only the code passed to Inline is used to create bindings.) ?

<!--[if !supportLists]-->5 <!--[endif]-->C Configuration Options

AUTO_INCLUDE

AUTOWRAP

<!--[if !supportLists]-->6 <!--[endif]-->C-Perl Bindings and The Inline Stack Macros

<!--[if !supportLists]-->7 <!--[endif]-->Writing C Subroutines

The definitions of your C functions will fall into one of the following four categories. For each category there are special considerations.

1) int Foo(int arg1, char* arg2, SV* arg3) {
2) void Foo(int arg1, char* arg2, SV* arg3) {
3) char* Foo(SV* arg1, ...) {
4) void* Foo(SV* arg1, ...) {

<!--[if !supportLists]-->8 <!--[endif]-->Examples

# !c:\perl\bin\perl-w

use strict;

use InlineC => ' DATA ' ;



greetStr(
' omohe ' );

greetStr(
' 65 ' );



greetScalar(
" omohe " );

greetScalar(
' 65 ' );



__DATA__

__C__



voidgreetStr(char
* name){

printf ( " Hellostring%s! " , name);

/* 能够直接使用printf而不需要include < stdio . h > 是因为Perl的内部已经实现了包含这些头文件 */

}

voidgreetScalar(SV
* sv_name){

printf ( " Helloscalar%s! " , SvPVX(sv_name));

// SvPVXisaperlinternalAPI for convertingtheSVtostring

}

Notes: 如上是一个Inline C的示例程序

<!--[if !supportLists]-->9 <!--[endif]-->Inline::C-Cookbook (More Inline::C Examples)

http://www.penguin-soft.com/penguin/man/3/Inline::C-Cookbook.html


10. 一些实例:
# !c:perlinperl-w
use strict;
use InlineC => ' DATA ' ;

# Perlcodegoeshere...
print add( 10 , 5 );

__DATA__
__C__
/* Ccodegoeshere ...*/
int add( int x , int y){
return x * y;
}

调用Win32,显示窗体:
use InlineC => DATA =>
LIBS
=> ' -luser32 ' ;


$text = " @ARGV " || ' Inline.pmworkswithMSWin32.Scary... ' ;


WinBox(
' InlineTextBox ' , $text );


__END__
__C__


# include<windows.h>


int WinBox(char * Caption , char * Text){
return MessageBoxA( 0 , Text , Caption , 0 );
}

更多使用Inline::C的实例参看Inline::C-Cookbook

http://www.penguin-soft.com/penguin/man/3/Inline::C-Cookbook.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值