Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "C 3" | |
132 | .TH C 3 "2002-11-04" "perl v5.8.0" "User Contributed Perl Documentation" | |
133 | .SH "NAME" | |
134 | Inline::C \- Write Perl Subroutines in C | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | \&\f(CW\*(C`Inline::C\*(C'\fR is a module that allows you to write Perl subroutines in C. | |
138 | Since version 0.30 the Inline module supports multiple programming | |
139 | languages and each language has its own support module. This document | |
140 | describes how to use Inline with the C programming language. It also | |
141 | goes a bit into Perl C internals. | |
142 | .PP | |
143 | If you want to start working with programming examples right away, check | |
144 | out Inline::C\-Cookbook. For more information on Inline in general, | |
145 | see Inline. | |
146 | .SH "Usage" | |
147 | .IX Header "Usage" | |
148 | You never actually use \f(CW\*(C`Inline::C\*(C'\fR directly. It is just a support | |
149 | module for using \f(CW\*(C`Inline.pm\*(C'\fR with C. So the usage is always: | |
150 | .PP | |
151 | .Vb 1 | |
152 | \& use Inline C => ...; | |
153 | .Ve | |
154 | .PP | |
155 | or | |
156 | .PP | |
157 | .Vb 1 | |
158 | \& bind Inline C => ...; | |
159 | .Ve | |
160 | .SH "Function Definitions" | |
161 | .IX Header "Function Definitions" | |
162 | The Inline grammar for C recognizes certain function definitions (or | |
163 | signatures) in your C code. If a signature is recognized by Inline, then | |
164 | it will be available in Perl\-space. That is, Inline will generate the | |
165 | \&\*(L"glue\*(R" necessary to call that function as if it were a Perl subroutine. | |
166 | If the signature is not recognized, Inline will simply ignore it, with | |
167 | no complaints. It will not be available from Perl\-space, although it | |
168 | \&\fIwill\fR be available from C\-space. | |
169 | .PP | |
170 | Inline looks for ANSI/prototype style function definitions. They must be | |
171 | of the form: | |
172 | .PP | |
173 | .Vb 1 | |
174 | \& return-type function-name ( type-name-pairs ) { ... } | |
175 | .Ve | |
176 | .PP | |
177 | The most common types are: \f(CW\*(C`int\*(C'\fR, \f(CW\*(C`long\*(C'\fR, \f(CW\*(C`double\*(C'\fR, \f(CW\*(C`char*\*(C'\fR, and | |
178 | \&\f(CW\*(C`SV*\*(C'\fR. But you can use any type for which Inline can find a typemap. | |
179 | Inline uses the \f(CW\*(C`typemap\*(C'\fR file distributed with Perl as the default. | |
180 | You can specify more typemaps with the \s-1TYPEMAPS\s0 configuration option. | |
181 | .PP | |
182 | A return type of \f(CW\*(C`void\*(C'\fR may also be used. The following are examples of | |
183 | valid function definitions. | |
184 | .PP | |
185 | .Vb 5 | |
186 | \& int Foo(double num, char* str) { | |
187 | \& void Foo(double num, char* str) { | |
188 | \& SV* Foo() { | |
189 | \& void Foo(SV*, ...) { | |
190 | \& long Foo(int i, int j, ...) { | |
191 | .Ve | |
192 | .PP | |
193 | The following definitions would not be recognized: | |
194 | .PP | |
195 | .Vb 4 | |
196 | \& Foo(int i) { # no return type | |
197 | \& int Foo(float f) { # no (default) typemap for float | |
198 | \& int Foo(num, str) double num; char* str; { | |
199 | \& void Foo(void) { # void only valid for return type | |
200 | .Ve | |
201 | .PP | |
202 | Notice that Inline only looks for function \fIdefinitions\fR, not function | |
203 | \&\fIprototypes\fR. Definitions are the syntax directly preceeding a function | |
204 | body. Also Inline does not scan external files, like headers. Only the | |
205 | code passed to Inline is used to create bindings; although other | |
206 | libraries can linked in, and called from C\-space. | |
207 | .SH "C Configuration Options" | |
208 | .IX Header "C Configuration Options" | |
209 | For information on how to specify Inline configuration options, see | |
210 | Inline. This section describes each of the configuration options | |
211 | available for C. Most of the options correspond either to MakeMaker or | |
212 | \&\s-1XS\s0 options of the same name. See ExtUtils::MakeMaker and perlxs. | |
213 | .Sh "\s-1AUTO_INCLUDE\s0" | |
214 | .IX Subsection "AUTO_INCLUDE" | |
215 | Specifies extra statements to automatically included. They will be added | |
216 | onto the defaults. A newline char will be automatically added. | |
217 | .PP | |
218 | .Vb 1 | |
219 | \& use Inline C => Config => AUTO_INCLUDE => '#include "yourheader.h"'; | |
220 | .Ve | |
221 | .Sh "\s-1AUTOWRAP\s0" | |
222 | .IX Subsection "AUTOWRAP" | |
223 | If you '\s-1ENABLE\s0 => \s-1AUTOWRAP\s0', Inline::C will parse function declarations | |
224 | (prototype statements) in your C code. For each declaration it can bind | |
225 | to, it will create a dummy wrapper that will call the real function | |
226 | which may be in an external library. This is a nice convenience for | |
227 | functions that would otherwise just require an empty wrapper function. | |
228 | .PP | |
229 | This is similar to the base functionality you get from \f(CW\*(C`h2xs\*(C'\fR. It can | |
230 | be very useful for binding to external libraries. | |
231 | .Sh "\s-1BOOT\s0" | |
232 | .IX Subsection "BOOT" | |
233 | Specifies C code to be executed in the \s-1XS\s0 \s-1BOOT\s0 section. Corresponds to | |
234 | the \s-1XS\s0 parameter. | |
235 | .Sh "\s-1CC\s0" | |
236 | .IX Subsection "CC" | |
237 | Specify which compiler to use. | |
238 | .Sh "\s-1CCFLAGS\s0" | |
239 | .IX Subsection "CCFLAGS" | |
240 | Specify extra compiler flags. | |
241 | .Sh "\s-1FILTERS\s0" | |
242 | .IX Subsection "FILTERS" | |
243 | Allows you to specify a list of source code filters. If more than one is | |
244 | requested, be sure to group them with an array ref. The filters can | |
245 | either be subroutine references or names of filters provided by the | |
246 | supplementary Inline::Filters module. | |
247 | .PP | |
248 | Your source code will be filtered just before it is parsed by Inline. | |
249 | The \s-1MD5\s0 fingerprint is generated before filtering. Source code | |
250 | filters can be used to do things like stripping out \s-1POD\s0 | |
251 | documentation, pre-expanding #include statements or whatever else you | |
252 | please. For example: | |
253 | .PP | |
254 | .Vb 2 | |
255 | \& use Inline C => DATA => | |
256 | \& FILTERS => [Strip_POD => \e&MyFilter => Preprocess ]; | |
257 | .Ve | |
258 | .PP | |
259 | Filters are invoked in the order specified. See Inline::Filters for | |
260 | more information. | |
261 | .Sh "\s-1INC\s0" | |
262 | .IX Subsection "INC" | |
263 | Specifies an include path to use. Corresponds to the MakeMaker parameter. | |
264 | .PP | |
265 | .Vb 1 | |
266 | \& use Inline C => Config => INC => '-I/inc/path'; | |
267 | .Ve | |
268 | .Sh "\s-1LD\s0" | |
269 | .IX Subsection "LD" | |
270 | Specify which linker to use. | |
271 | .Sh "\s-1LDDLFLAGS\s0" | |
272 | .IX Subsection "LDDLFLAGS" | |
273 | Specify which linker flags to use. | |
274 | .PP | |
275 | \&\s-1NOTE:\s0 | |
276 | These flags will completely override the existing flags, instead of | |
277 | just adding to them. So if you need to use those too, you must | |
278 | respecify them here. | |
279 | .Sh "\s-1LIBS\s0" | |
280 | .IX Subsection "LIBS" | |
281 | Specifies external libraries that should be linked into your code. | |
282 | Corresponds to the MakeMaker parameter. | |
283 | .PP | |
284 | .Vb 1 | |
285 | \& use Inline C => Config => LIBS => '-lyourlib'; | |
286 | .Ve | |
287 | .PP | |
288 | or | |
289 | .PP | |
290 | .Vb 1 | |
291 | \& use Inline C => Config => LIBS => '-L/your/path -lyourlib'; | |
292 | .Ve | |
293 | .Sh "\s-1MAKE\s0" | |
294 | .IX Subsection "MAKE" | |
295 | Specify the name of the 'make' utility to use. | |
296 | .Sh "\s-1MYEXTLIB\s0" | |
297 | .IX Subsection "MYEXTLIB" | |
298 | Specifies a user compiled object that should be linked in. Corresponds | |
299 | to the MakeMaker parameter. | |
300 | .PP | |
301 | .Vb 1 | |
302 | \& use Inline C => Config => MYEXTLIB => '/your/path/yourmodule.so'; | |
303 | .Ve | |
304 | .Sh "\s-1OPTIMIZE\s0" | |
305 | .IX Subsection "OPTIMIZE" | |
306 | This controls the MakeMaker \s-1OPTIMIZE\s0 setting. By setting this value to | |
307 | \&\f(CW'\-g'\fR, you can turn on debugging support for your Inline extensions. | |
308 | This will allow you to be able to set breakpoints in your C code using a | |
309 | debugger like gdb. | |
310 | .Sh "\s-1PREFIX\s0" | |
311 | .IX Subsection "PREFIX" | |
312 | Specifies a prefix that will be automatically stripped from C functions | |
313 | when they are bound to Perl. Useful for creating wrappers for shared | |
314 | library API\-s, and binding to the original names in Perl. Also useful | |
315 | when names conflict with Perl internals. Corresponds to the \s-1XS\s0 | |
316 | parameter. | |
317 | .PP | |
318 | .Vb 1 | |
319 | \& use Inline C => Config => PREFIX => 'ZLIB_'; | |
320 | .Ve | |
321 | .Sh "\s-1TYPEMAPS\s0" | |
322 | .IX Subsection "TYPEMAPS" | |
323 | Specifies extra typemap files to use. These types will modify the | |
324 | behaviour of the C parsing. Corresponds to the MakeMaker parameter. | |
325 | .PP | |
326 | .Vb 1 | |
327 | \& use Inline C => Config => TYPEMAPS => '/your/path/typemap'; | |
328 | .Ve | |
329 | .SH "C\-Perl Bindings" | |
330 | .IX Header "C-Perl Bindings" | |
331 | This section describes how the \f(CW\*(C`Perl\*(C'\fR variables get mapped to \f(CW\*(C`C\*(C'\fR | |
332 | variables and back again. | |
333 | .PP | |
334 | First, you need to know how \f(CW\*(C`Perl\*(C'\fR passes arguments back and forth to | |
335 | subroutines. Basically it uses a stack (also known as the \fBStack\fR). | |
336 | When a sub is called, all of the parenthesized arguments get expanded | |
337 | into a list of scalars and pushed onto the \fBStack\fR. The subroutine then | |
338 | pops all of its parameters off of the \fBStack\fR. When the sub is done, it | |
339 | pushes all of its return values back onto the \fBStack\fR. | |
340 | .PP | |
341 | The \fBStack\fR is an array of scalars known internally as \f(CW\*(C`SV\*(C'\fR's. The | |
342 | \&\fBStack\fR is actually an array of \fBpointers to \s-1SV\s0\fR or \f(CW\*(C`SV*\*(C'\fR; therefore | |
343 | every element of the \fBStack\fR is natively a \f(CW\*(C`SV*\*(C'\fR. For \fI\s-1FMTYEWTK\s0\fR | |
344 | about this, read \f(CW\*(C`perldoc perlguts\*(C'\fR. | |
345 | .PP | |
346 | So back to variable mapping. \s-1XS\s0 uses a thing known as \*(L"typemaps\*(R" to turn | |
347 | each \f(CW\*(C`SV*\*(C'\fR into a \f(CW\*(C`C\*(C'\fR type and back again. This is done through | |
348 | various \s-1XS\s0 macro calls, casts and the Perl \s-1API\s0. See \f(CW\*(C`perldoc perlapi\*(C'\fR. | |
349 | \&\s-1XS\s0 allows you to define your own typemaps as well for fancier | |
350 | non-standard types such as \f(CW\*(C`typedef\*(C'\fR\-ed structs. | |
351 | .PP | |
352 | Inline uses the default Perl typemap file for its default types. This | |
353 | file is called \f(CW\*(C`/usr/local/lib/perl5/5.6.1/ExtUtils/typemap\*(C'\fR, or | |
354 | something similar, depending on your Perl installation. It has | |
355 | definitions for over 40 types, which are automatically used by Inline. | |
356 | (You should probably browse this file at least once, just to get an idea | |
357 | of the possibilities.) | |
358 | .PP | |
359 | Inline parses your code for these types and generates the \s-1XS\s0 code to map | |
360 | them. The most commonly used types are: | |
361 | .PP | |
362 | .Vb 6 | |
363 | \& - int | |
364 | \& - long | |
365 | \& - double | |
366 | \& - char* | |
367 | \& - void | |
368 | \& - SV* | |
369 | .Ve | |
370 | .PP | |
371 | If you need to deal with a type that is not in the defaults, just | |
372 | use the generic \f(CW\*(C`SV*\*(C'\fR type in the function definition. Then inside | |
373 | your code, do the mapping yourself. Alternatively, you can create | |
374 | your own typemap files and specify them using the \f(CW\*(C`TYPEMAPS\*(C'\fR | |
375 | configuration option. | |
376 | .PP | |
377 | A return type of \f(CW\*(C`void\*(C'\fR has a special meaning to Inline. It means that | |
378 | you plan to push the values back onto the \fBStack\fR yourself. This is | |
379 | what you need to do to return a list of values. If you really don't want | |
380 | to return anything (the traditional meaning of \f(CW\*(C`void\*(C'\fR) then simply | |
381 | don't push anything back. | |
382 | .PP | |
383 | If ellipsis or \f(CW\*(C`...\*(C'\fR is used at the end of an argument list, it means | |
384 | that any number of \f(CW\*(C`SV*\*(C'\fRs may follow. Again you will need to pop the | |
385 | values off of the \f(CW\*(C`Stack\*(C'\fR yourself. | |
386 | .PP | |
387 | See \*(L"Examples\*(R" below. | |
388 | .SH "The Inline Stack Macros" | |
389 | .IX Header "The Inline Stack Macros" | |
390 | When you write Inline C, the following lines are automatically prepended | |
391 | to your code (by default): | |
392 | .PP | |
393 | .Vb 4 | |
394 | \& #include "EXTERN.h" | |
395 | \& #include "perl.h" | |
396 | \& #include "XSUB.h" | |
397 | \& #include "INLINE.h" | |
398 | .Ve | |
399 | .PP | |
400 | The file \f(CW\*(C`INLINE.h\*(C'\fR defines a set of macros that are useful for | |
401 | handling the Perl Stack from your C functions. | |
402 | .IP "Inline_Stack_Vars" 4 | |
403 | .IX Item "Inline_Stack_Vars" | |
404 | You'll need to use this one, if you want to use the others. It sets up a | |
405 | few local variables: \f(CW\*(C`sp\*(C'\fR, \f(CW\*(C`items\*(C'\fR, \f(CW\*(C`ax\*(C'\fR and \f(CW\*(C`mark\*(C'\fR, for use by the | |
406 | other macros. It's not important to know what they do, but I mention | |
407 | them to avoid possible name conflicts. | |
408 | .Sp | |
409 | \&\s-1NOTE:\s0 | |
410 | Since this macro declares variables, you'll need to put it with your | |
411 | other variable declarations at the top of your function. It must | |
412 | come before any executable statements and before any other | |
413 | \&\f(CW\*(C`Inline_Stack\*(C'\fR macros. | |
414 | .IP "Inline_Stack_Items" 4 | |
415 | .IX Item "Inline_Stack_Items" | |
416 | Returns the number of arguments passed in on the Stack. | |
417 | .IP "Inline_Stack_Item(i)" 4 | |
418 | .IX Item "Inline_Stack_Item(i)" | |
419 | Refers to a particular \f(CW\*(C`SV*\*(C'\fR in the Stack, where \f(CW\*(C`i\*(C'\fR is an index | |
420 | number starting from zero. Can be used to get or set the value. | |
421 | .IP "Inline_Stack_Reset" 4 | |
422 | .IX Item "Inline_Stack_Reset" | |
423 | Use this before pushing anything back onto the Stack. It resets the | |
424 | internal Stack pointer to the beginning of the Stack. | |
425 | .IP "Inline_Stack_Push(sv)" 4 | |
426 | .IX Item "Inline_Stack_Push(sv)" | |
427 | Push a return value back onto the Stack. The value must be of type \f(CW\*(C`SV*\*(C'\fR. | |
428 | .IP "Inline_Stack_Done" 4 | |
429 | .IX Item "Inline_Stack_Done" | |
430 | After you have pushed all of your return values, you must call this macro. | |
431 | .IP "Inline_Stack_Return(n)" 4 | |
432 | .IX Item "Inline_Stack_Return(n)" | |
433 | Return \f(CW\*(C`n\*(C'\fR items on the Stack. | |
434 | .IP "Inline_Stack_Void" 4 | |
435 | .IX Item "Inline_Stack_Void" | |
436 | A special macro to indicate that you really don't want to return | |
437 | anything. Same as: | |
438 | .Sp | |
439 | .Vb 1 | |
440 | \& Inline_Stack_Return(0); | |
441 | .Ve | |
442 | .Sp | |
443 | Please note that this macro actually \fBreturns\fR from your function. | |
444 | .PP | |
445 | Each of these macros is available in 3 different styles to suit your | |
446 | coding tastes. The following macros are equivalent. | |
447 | .PP | |
448 | .Vb 3 | |
449 | \& Inline_Stack_Vars | |
450 | \& inline_stack_vars | |
451 | \& INLINE_STACK_VARS | |
452 | .Ve | |
453 | .PP | |
454 | All of this functionality is available through \s-1XS\s0 macro calls as well. | |
455 | So why duplicate the functionality? There are a few reasons why I | |
456 | decided to offer this set of macros. First, as a convenient way to | |
457 | access the Stack. Second, for consistent, self documenting, non-cryptic | |
458 | coding. Third, for future compatibility. It occured to me that if a lot | |
459 | of people started using \s-1XS\s0 macros for their C code, the interface might | |
460 | break under Perl6. By using this set, hopefully I will be able to insure | |
461 | future compatibility of argument handling. | |
462 | .PP | |
463 | Of course, if you use the rest of the Perl \s-1API\s0, your code will most | |
464 | likely break under Perl6. So this is not a 100% guarantee. But since | |
465 | argument handling is the most common interface you're likely to use, it | |
466 | seemed like a wise thing to do. | |
467 | .SH "Writing C Subroutines" | |
468 | .IX Header "Writing C Subroutines" | |
469 | The definitions of your C functions will fall into one of the following | |
470 | four categories. For each category there are special considerations. | |
471 | .IP "1" 4 | |
472 | .IX Item "1" | |
473 | .Vb 1 | |
474 | \& int Foo(int arg1, char* arg2, SV* arg3) { | |
475 | .Ve | |
476 | .Sp | |
477 | This is the simplest case. You have a non \f(CW\*(C`void\*(C'\fR return type and a | |
478 | fixed length argument list. You don't need to worry about much. All the | |
479 | conversions will happen automatically. | |
480 | .IP "2" 4 | |
481 | .IX Item "2" | |
482 | .Vb 1 | |
483 | \& void Foo(int arg1, char* arg2, SV* arg3) { | |
484 | .Ve | |
485 | .Sp | |
486 | In this category you have a \f(CW\*(C`void\*(C'\fR return type. This means that either | |
487 | you want to return nothing, or that you want to return a list. In the | |
488 | latter case you'll need to push values onto the \fBStack\fR yourself. There | |
489 | are a few Inline macros that make this easy. Code something like this: | |
490 | .Sp | |
491 | .Vb 6 | |
492 | \& int i, max; SV* my_sv[10]; | |
493 | \& Inline_Stack_Vars; | |
494 | \& Inline_Stack_Reset; | |
495 | \& for (i = 0; i < max; i++) | |
496 | \& Inline_Stack_Push(my_sv[i]); | |
497 | \& Inline_Stack_Done; | |
498 | .Ve | |
499 | .Sp | |
500 | After resetting the Stack pointer, this code pushes a series of return | |
501 | values. At the end it uses \f(CW\*(C`Inline_Stack_Done\*(C'\fR to mark the end of the | |
502 | return stack. | |
503 | .Sp | |
504 | If you really want to return nothing, then don't use the | |
505 | \&\f(CW\*(C`Inline_Stack_\*(C'\fR macros. If you must use them, then set use | |
506 | \&\f(CW\*(C`Inline_Stack_Void\*(C'\fR at the end of your function. | |
507 | .IP "3" 4 | |
508 | .IX Item "3" | |
509 | .Vb 1 | |
510 | \& char* Foo(SV* arg1, ...) { | |
511 | .Ve | |
512 | .Sp | |
513 | In this category you have an unfixed number of arguments. This | |
514 | means that you'll have to pop values off the \fBStack\fR yourself. Do | |
515 | it like this: | |
516 | .Sp | |
517 | .Vb 4 | |
518 | \& int i; | |
519 | \& Inline_Stack_Vars; | |
520 | \& for (i = 0; i < Inline_Stack_Items; i++) | |
521 | \& handle_sv(Inline_Stack_Item(i)); | |
522 | .Ve | |
523 | .Sp | |
524 | The return type of \f(CWInline_Stack_Item(i)\fR is \f(CW\*(C`SV*\*(C'\fR. | |
525 | .IP "4" 4 | |
526 | .IX Item "4" | |
527 | .Vb 1 | |
528 | \& void* Foo(SV* arg1, ...) { | |
529 | .Ve | |
530 | .Sp | |
531 | In this category you have both a \f(CW\*(C`void\*(C'\fR return type and an | |
532 | unfixed number of arguments. Just combine the techniques from | |
533 | Categories 3 and 4. | |
534 | .SH "Examples" | |
535 | .IX Header "Examples" | |
536 | Here are a few examples. Each one is a complete program that you can try | |
537 | running yourself. For many more examples see Inline::C\-Cookbook. | |
538 | .Sh "Example #1 \- Greetings" | |
539 | .IX Subsection "Example #1 - Greetings" | |
540 | This example will take one string argument (a name) and print a | |
541 | greeting. The function is called with a string and with a number. In the | |
542 | second case the number is forced to a string. | |
543 | .PP | |
544 | Notice that you do not need to \f(CW\*(C`#include <stdio.h\*(C'\fR>. The \f(CW\*(C`perl.h\*(C'\fR | |
545 | header file which gets included by default, automatically loads the | |
546 | standard C header files for you. | |
547 | .PP | |
548 | .Vb 8 | |
549 | \& use Inline C; | |
550 | \& greet('Ingy'); | |
551 | \& greet(42); | |
552 | \& __END__ | |
553 | \& __C__ | |
554 | \& void greet(char* name) { | |
555 | \& printf("Hello %s!\en", name); | |
556 | \& } | |
557 | .Ve | |
558 | .Sh "Example #2 \- and Salutations" | |
559 | .IX Subsection "Example #2 - and Salutations" | |
560 | This is similar to the last example except that the name is passed in as | |
561 | a \f(CW\*(C`SV*\*(C'\fR (pointer to Scalar Value) rather than a string (\f(CW\*(C`char*\*(C'\fR). That | |
562 | means we need to convert the \f(CW\*(C`SV\*(C'\fR to a string ourselves. This is | |
563 | accomplished using the \f(CW\*(C`SvPVX\*(C'\fR function which is part of the \f(CW\*(C`Perl\*(C'\fR | |
564 | internal \s-1API\s0. See \f(CW\*(C`perldoc perlapi\*(C'\fR for more info. | |
565 | .PP | |
566 | One problem is that \f(CW\*(C`SvPVX\*(C'\fR doesn't automatically convert strings | |
567 | to numbers, so we get a little surprise when we try to greet \f(CW42\fR. | |
568 | The program segfaults, a common occurence when delving into the | |
569 | guts of Perl. | |
570 | .PP | |
571 | .Vb 8 | |
572 | \& use Inline C; | |
573 | \& greet('Ingy'); | |
574 | \& greet(42); | |
575 | \& __END__ | |
576 | \& __C__ | |
577 | \& void greet(SV* sv_name) { | |
578 | \& printf("Hello %s!\en", SvPVX(sv_name)); | |
579 | \& } | |
580 | .Ve | |
581 | .Sh "Example #3 \- Fixing the problem" | |
582 | .IX Subsection "Example #3 - Fixing the problem" | |
583 | We can fix the problem in Example #2 by using the \f(CW\*(C`SvPV\*(C'\fR function | |
584 | instead. This function will stringify the \f(CW\*(C`SV\*(C'\fR if it does not contain a | |
585 | string. \f(CW\*(C`SvPV\*(C'\fR returns the length of the string as it's second | |
586 | parameter. Since we don't care about the length, we can just put | |
587 | \&\f(CW\*(C`PL_na\*(C'\fR there, which is a special variable designed for that purpose. | |
588 | .PP | |
589 | .Vb 8 | |
590 | \& use Inline C; | |
591 | \& greet('Ingy'); | |
592 | \& greet(42); | |
593 | \& __END__ | |
594 | \& __C__ | |
595 | \& void greet(SV* sv_name) { | |
596 | \& printf("Hello %s!\en", SvPV(sv_name, PL_na)); | |
597 | \& } | |
598 | .Ve | |
599 | .SH "SEE ALSO" | |
600 | .IX Header "SEE ALSO" | |
601 | For general information about Inline see Inline. | |
602 | .PP | |
603 | For sample programs using Inline with C see Inline::C\-Cookbook. | |
604 | .PP | |
605 | For information on supported languages and platforms see | |
606 | Inline-Support. | |
607 | .PP | |
608 | For information on writing your own Inline Language Support Module, see | |
609 | Inline-API. | |
610 | .PP | |
611 | Inline's mailing list is inline@perl.org | |
612 | .PP | |
613 | To subscribe, send email to inline\-subscribe@perl.org | |
614 | .SH "BUGS AND DEFICIENCIES" | |
615 | .IX Header "BUGS AND DEFICIENCIES" | |
616 | .IP "1" 4 | |
617 | .IX Item "1" | |
618 | If you use C function names that happen to be used internally by Perl, | |
619 | you will get a load error at run time. There is currently no | |
620 | functionality to prevent this or to warn you. For now, a list of Perl's | |
621 | internal symbols is packaged in the Inline module distribution under the | |
622 | filename \f(CW'symbols.perl'\fR. Avoid using these in your code. | |
623 | .SH "AUTHOR" | |
624 | .IX Header "AUTHOR" | |
625 | Brian Ingerson <INGY@cpan.org> | |
626 | .SH "COPYRIGHT" | |
627 | .IX Header "COPYRIGHT" | |
628 | Copyright (c) 2000, 2001, 2002. Brian Ingerson. All rights reserved. | |
629 | .PP | |
630 | This program is free software; you can redistribute it and/or modify it | |
631 | under the same terms as Perl itself. | |
632 | .PP | |
633 | See http://www.perl.com/perl/misc/Artistic.html |