1Inline(3) User Contributed Perl Documentation Inline(3)
2
3
4
6 Inline - Write Perl subroutines in other programming languages.
7
9 use Inline C;
10
11 print "9 + 16 = ", add(9, 16), "\n";
12 print "9 - 16 = ", subtract(9, 16), "\n";
13
14 __END__
15 __C__
16 int add(int x, int y) {
17 return x + y;
18 }
19
20 int subtract(int x, int y) {
21 return x - y;
22 }
23
25 The Inline module allows you to put source code from other programming
26 languages directly "inline" in a Perl script or module. The code is
27 automatically compiled as needed, and then loaded for immediate access
28 from Perl.
29
30 Inline saves you from the hassle of having to write and compile your
31 own glue code using facilities like XS or SWIG. Simply type the code
32 where you want it and run your Perl as normal. All the hairy details
33 are handled for you. The compilation and installation of your code
34 chunks all happen transparently; all you will notice is the delay of
35 compilation on the first run.
36
37 The Inline code only gets compiled the first time you run it (or when‐
38 ever it is modified) so you only take the performance hit once. Code
39 that is Inlined into distributed modules (like on the CPAN) will get
40 compiled when the module is installed, so the end user will never
41 notice the compilation time.
42
43 Best of all, it works the same on both Unix and Microsoft Windows. See
44 Inline-Support for support information.
45
46 Why Inline?
47
48 Do you want to know "Why would I use other languages in Perl?" or "Why
49 should I use Inline to do it?"? I'll try to answer both.
50
51 Why would I use other languages in Perl?
52 The most obvious reason is performance. For an interpreted lan‐
53 guage, Perl is very fast. Many people will say "Anything Perl can
54 do, C can do faster". (They never mention the development time :-)
55 Anyway, you may be able to remove a bottleneck in your Perl code by
56 using another language, without having to write the entire program
57 in that language. This keeps your overall development time down,
58 because you're using Perl for all of the non-critical code.
59
60 Another reason is to access functionality from existing API-s that
61 use the language. Some of this code may only be available in binary
62 form. But by creating small subroutines in the native language,
63 you can "glue" existing libraries to your Perl. As a user of the
64 CPAN, you know that code reuse is a good thing. So why throw away
65 those Fortran libraries just yet?
66
67 If you are using Inline with the C language, then you can access
68 the full internals of Perl itself. This opens up the floodgates to
69 both extreme power and peril.
70
71 Maybe the best reason is "Because you want to!". Diversity keeps
72 the world interesting. TMTOWTDI!
73
74 Why should I use Inline to do it?
75 There are already two major facilities for extending Perl with C.
76 They are XS and SWIG. Both are similar in their capabilities, at
77 least as far as Perl is concerned. And both of them are quite dif‐
78 ficult to learn compared to Inline.
79
80 There is a big fat learning curve involved with setting up and
81 using the XS environment. You need to get quite intimate with the
82 following docs:
83
84 * perlxs
85 * perlxstut
86 * perlapi
87 * perlguts
88 * perlmod
89 * h2xs
90 * xsubpp
91 * ExtUtils::MakeMaker
92
93 With Inline you can be up and running in minutes. There is a C
94 Cookbook with lots of short but complete programs that you can
95 extend to your real-life problems. No need to learn about the com‐
96 plicated build process going on in the background. You don't even
97 need to compile the code yourself. Inline takes care of every last
98 detail except writing the C code.
99
100 Perl programmers cannot be bothered with silly things like compil‐
101 ing. "Tweak, Run, Tweak, Run" is our way of life. Inline does all
102 the dirty work for you.
103
104 Another advantage of Inline is that you can use it directly in a
105 script. You can even use it in a Perl one-liner. With XS and SWIG,
106 you always set up an entirely separate module. Even if you only
107 have one or two functions. Inline makes easy things easy, and hard
108 things possible. Just like Perl.
109
110 Finally, Inline supports several programming languages (not just C
111 and C++). As of this writing, Inline has support for C, C++, Java,
112 Python, Ruby, Tcl, Assembler, Basic, Guile, Befunge, Octave, Awk,
113 BC, TT (Template Toolkit), WebChat and even PERL. New Inline Lan‐
114 guage Support Modules (ILSMs) are regularly being added. See
115 Inline-API for details on how to create your own ILSM.
116
118 Inline is a little bit different than most of the Perl modules that you
119 are used to. It doesn't import any functions into your namespace and it
120 doesn't have any object oriented methods. Its entire interface (with
121 two minor exceptions) is specified through the 'use Inline ...' com‐
122 mand.
123
124 This section will explain all of the different ways to "use Inline". If
125 you want to begin using C with Inline immediately, see Inline::C-Cook‐
126 book.
127
128 The Basics
129
130 The most basic form for using Inline is:
131
132 use Inline X => "X source code";
133
134 where 'X' is one of the supported Inline programming languages. The
135 second parameter identifies the source code that you want to bind to
136 Perl. The source code can be specified using any of the following syn‐
137 taxes:
138
139 The DATA Keyword.
140 use Inline Java => 'DATA';
141
142 # Perl code goes here ...
143
144 __DATA__
145 __Java__
146 /* Java code goes here ... */
147
148 The easiest and most visually clean way to specify your source code
149 in an Inline Perl program is to use the special "DATA" keyword.
150 This tells Inline to look for a special marker in your "DATA" file‐
151 handle's input stream. In this example the special marker is
152 "__Java__", which is the programming language surrounded by double
153 underscores.
154
155 In case you've forgotten, the "DATA" pseudo file is comprised of
156 all the text after the "__END__" or "__DATA__" section of your pro‐
157 gram. If you're working outside the "main" package, you'd best use
158 the "__DATA__" marker or else Inline will not find your code.
159
160 Using this scheme keeps your Perl code at the top, and all the ugly
161 Java stuff down below where it belongs. This is visually clean and
162 makes for more maintainable code. An excellent side benefit is that
163 you don't have to escape any characters like you might in a Perl
164 string. The source code is verbatim. For these reasons, I prefer
165 this method the most.
166
167 The only problem with this style is that since Perl can't read the
168 "DATA" filehandle until runtime, it obviously can't bind your func‐
169 tions until runtime. The net effect of this is that you can't use
170 your Inline functions as barewords (without predeclaring them)
171 because Perl has no idea they exist during compile time.
172
173 The FILE and BELOW keywords.
174 use Inline::Files;
175 use Inline Java => 'FILE';
176
177 # Perl code goes here ...
178
179 __JAVA__
180 /* Java code goes here ... */
181
182 This is the newest method of specifying your source code. It makes
183 use of the Perl module "Inline::Files" written by Damian Conway.
184 The basic style and meaning are the same as for the "DATA" keyword,
185 but there are a few syntactic and semantic twists.
186
187 First, you must say 'use Inline::Files' before you 'use Inline'
188 code that needs those files. The special '"DATA"' keyword is
189 replaced by either '"FILE"' or '"BELOW"'. This allows for the bad
190 pun idiom of:
191
192 use Inline C => 'BELOW';
193
194 You can omit the "__DATA__" tag now. Inline::Files is a source fil‐
195 ter that will remove these sections from your program before Perl
196 compiles it. They are then available for Inline to make use of. And
197 since this can all be done at compile time, you don't have to worry
198 about the caveats of the 'DATA' keyword.
199
200 This module has a couple small gotchas. Since Inline::Files only
201 recognizes file markers with capital letters, you must specify the
202 capital form of your language name. Also, there is a startup time
203 penalty for using a source code filter.
204
205 At this point Inline::Files is alpha software and use of it is
206 experimental. Inline's integration of this module is also fledgling
207 at the time being. One of things I plan to do with Inline::Files is
208 to get line number info so when an extension doesn't compile, the
209 error messages will point to the correct source file and line num‐
210 ber.
211
212 My best advice is to use Inline::Files for testing (especially as
213 support for it improves), but use DATA for production and distrib‐
214 uted/CPAN code.
215
216 Strings
217 use Inline Java => <<'END';
218
219 /* Java code goes here ... */
220 END
221
222 # Perl code goes here ...
223
224 You also just specify the source code as a single string. A handy
225 way to write the string is to use Perl's "here document" style of
226 quoting. This is ok for small functions but can get unwieldy in the
227 large. On the other hand, the string variant probably has the least
228 startup penalty and all functions are bound at compile time.
229
230 If you wish to put the string into a scalar variable, please be
231 aware that the "use" statement is a compile time directive. As
232 such, all the variables it uses must also be set at compile time,
233 "before" the 'use Inline' statement. Here is one way to do it:
234
235 my $code;
236 BEGIN {
237 $code = <<END;
238
239 /* Java code goes here ... */
240 END
241 }
242 use Inline Java => $code;
243
244 # Perl code goes here ...
245
246 The bind() Function
247 An alternative to using the BEGIN block method is to specify the
248 source code at run time using the 'Inline->bind()' method. (This is
249 one of the interface exceptions mentioned above) The "bind()"
250 method takes the same arguments as 'use Inline ...'.
251
252 my $code = <<END;
253
254 /* Java code goes here ... */
255 END
256
257 Inline->bind(Java => $code);
258
259 You can think of "bind()" as a way to "eval()" code in other pro‐
260 gramming languages.
261
262 Although bind() is a powerful feature, it is not recommended for
263 use in Inline based modules. In fact, it won't work at all for
264 installable modules. See instructions below for creating modules
265 with Inline.
266
267 Other Methods
268 The source code for Inline can also be specified as an external
269 filename, a reference to a subroutine that returns source code, or
270 a reference to an array that contains lines of source code. These
271 methods are less frequently used but may be useful in some situa‐
272 tions.
273
274 Shorthand
275 If you are using the 'DATA' or 'FILE' methods described above and
276 there are no extra parameters, you can omit the keyword altogether.
277 For example:
278
279 use Inline 'Java';
280
281 # Perl code goes here ...
282
283 __DATA__
284 __Java__
285 /* Java code goes here ... */
286
287 or
288
289 use Inline::Files;
290 use Inline 'Java';
291
292 # Perl code goes here ...
293
294 __JAVA__
295 /* Java code goes here ... */
296
297 More about the DATA Section
298
299 If you are writing a module, you can also use the DATA section for POD
300 and AutoLoader subroutines. Just be sure to put them before the first
301 Inline marker. If you install the helper module "Inline::Filters", you
302 can even use POD inside your Inline code. You just have to specify a
303 filter to strip it out.
304
305 You can also specify multiple Inline sections, possibly in different
306 programming languages. Here is another example:
307
308 # The module Foo.pm
309 package Foo;
310 use AutoLoader;
311
312 use Inline C;
313 use Inline C => DATA => FILTERS => 'Strip_POD';
314 use Inline Python;
315
316 1;
317
318 __DATA__
319
320 sub marine {
321 # This is an autoloaded subroutine
322 }
323
324 =head1 External subroutines
325
326 =cut
327
328 __C__
329 /* First C section */
330
331 __C__
332 /* Second C section */
333 =head1 My C Function
334
335 Some POD doc.
336
337 =cut
338
339 __Python__
340 """A Python Section"""
341
342 An important thing to remember is that you need to have one "use Inline
343 Foo => 'DATA'" for each "__Foo__" marker, and they must be in the same
344 order. This allows you to apply different configuration options to each
345 section.
346
347 Configuration Options
348
349 Inline trys to do the right thing as often as possible. But sometimes
350 you may need to override the default actions. This is easy to do. Sim‐
351 ply list the Inline configuration options after the regular Inline
352 parameters. All congiguration options are specified as (key, value)
353 pairs.
354
355 use Inline (C => 'DATA',
356 DIRECTORY => './inline_dir',
357 LIBS => '-lfoo',
358 INC => '-I/foo/include',
359 PREFIX => 'XXX_',
360 WARNINGS => 0,
361 );
362
363 You can also specify the configuration options on a separate Inline
364 call like this:
365
366 use Inline (C => Config =>
367 DIRECTORY => './inline_dir',
368 LIBS => '-lfoo',
369 INC => '-I/foo/include',
370 PREFIX => 'XXX_',
371 WARNINGS => 0,
372 );
373 use Inline C => <<'END_OF_C_CODE';
374
375 The special keyword 'Config' tells Inline that this is a configuration-
376 only call. No source code will be compiled or bound to Perl.
377
378 If you want to specify global configuration options that don't apply to
379 a particular language, just leave the language out of the call. Like
380 this:
381
382 use Inline Config => WARNINGS => 0;
383
384 The Config options are inherited and additive. You can use as many Con‐
385 fig calls as you want. And you can apply different options to different
386 code sections. When a source code section is passed in, Inline will
387 apply whichever options have been specified up to that point. Here is a
388 complex configuration example:
389
390 use Inline (Config =>
391 DIRECTORY => './inline_dir',
392 );
393 use Inline (C => Config =>
394 LIBS => '-lglobal',
395 );
396 use Inline (C => 'DATA', # First C Section
397 LIBS => ['-llocal1', '-llocal2'],
398 );
399 use Inline (Config =>
400 WARNINGS => 0,
401 );
402 use Inline (Python => 'DATA', # First Python Section
403 LIBS => '-lmypython1',
404 );
405 use Inline (C => 'DATA', # Second C Section
406 LIBS => [undef, '-llocal3'],
407 );
408
409 The first "Config" applies to all subsequent calls. The second "Config"
410 applies to all subsequent "C" sections (but not "Python" sections). In
411 the first "C" section, the external libraries "global", "local1" and
412 "local2" are used. (Most options allow either string or array ref
413 forms, and do the right thing.) The "Python" section does not use the
414 "global" library, but does use the same "DIRECTORY", and has warnings
415 turned off. The second "C" section only uses the "local3" library.
416 That's because a value of "undef" resets the additive behavior.
417
418 The "DIRECTORY" and "WARNINGS" options are generic Inline options. All
419 other options are language specific. To find out what the "C" options
420 do, see "Inline::C".
421
422 On and Off
423
424 If a particular config option has value options of 1 and 0, you can use
425 the ENABLE and DISABLE modifiers. In other words, this:
426
427 use Inline Config =>
428 FORCE_BUILD => 1,
429 CLEAN_AFTER_BUILD => 0;
430
431 could be reworded as:
432
433 use Inline Config =>
434 ENABLE => FORCE_BUILD,
435 DISABLE => CLEAN_AFTER_BUILD;
436
437 Playing 'with' Others
438
439 Inline has a special configuration syntax that tells it to get more
440 configuration options from other Perl modules. Here is an example:
441
442 use Inline with => 'Event';
443
444 This tells Inline to load the module "Event.pm" and ask it for configu‐
445 ration information. Since "Event" has a C API of its own, it can pass
446 Inline all of the information it needs to be able to use "Event" C
447 callbacks seamlessly.
448
449 That means that you don't need to specify the typemaps, shared
450 libraries, include files and other information required to get this to
451 work.
452
453 You can specify a single module or a list of them. Like:
454
455 use Inline with => qw(Event Foo Bar);
456
457 Currently, "Event" is the only module that works with Inline.
458
459 Inline Shortcuts
460
461 Inline lets you set many configuration options from the command line.
462 These options are called 'shortcuts'. They can be very handy, espe‐
463 cially when you only want to set the options temporarily, for say,
464 debugging.
465
466 For instance, to get some general information about your Inline code in
467 the script "Foo.pl", use the command:
468
469 perl -MInline=INFO Foo.pl
470
471 If you want to force your code to compile, even if its already done,
472 use:
473
474 perl -MInline=FORCE Foo.pl
475
476 If you want to do both, use:
477
478 perl -MInline=INFO -MInline=FORCE Foo.pl
479
480 or better yet:
481
482 perl -MInline=INFO,FORCE Foo.pl
483
484 The Inline DIRECTORY
485
486 Inline needs a place to build your code and to install the results of
487 the build. It uses a single directory named '.Inline/' under normal
488 circumstances. If you create this directory in your home directory, the
489 current directory or in the directory where your program resides,
490 Inline will find and use it. You can also specify it in the environment
491 variable "PERL_INLINE_DIRECTORY" or directly in your program, by using
492 the "DIRECTORY" keyword option. If Inline cannot find the directory in
493 any of these places it will create a '_Inline/' directory in either
494 your current directory or the directory where your script resides.
495
496 One of the key factors to using Inline successfully, is understanding
497 this directory. When developing code it is usually best to create this
498 directory (or let Inline do it) in your current directory. Remember
499 that there is nothing sacred about this directory except that it holds
500 your compiled code. Feel free to delete it at any time. Inline will
501 simply start from scratch and recompile your code on the next run. If
502 you have several programs that you want to force to recompile, just
503 delete your '.Inline/' directory.
504
505 It is probably best to have a separate '.Inline/' directory for each
506 project that you are working on. You may want to keep stable code in
507 the <.Inline/> in your home directory. On multi-user systems, each user
508 should have their own '.Inline/' directories. It could be a security
509 risk to put the directory in a shared place like "/tmp/".
510
511 Debugging Inline Errors
512
513 All programmers make mistakes. When you make a mistake with Inline,
514 like writing bad C code, you'll get a big error report on your screen.
515 This report tells you where to look to do the debugging. Some languages
516 may also dump out the error messages generated from the build.
517
518 When Inline needs to build something it creates a subdirectory under
519 your "DIRECTORY/build/" directory. This is where it writes all the com‐
520 ponents it needs to build your extension. Things like XS files, Make‐
521 files and output log files.
522
523 If everything goes OK, Inline will delete this subdirectory. If there
524 is an error, Inline will leave the directory intact and print its loca‐
525 tion. The idea is that you are supposed to go into that directory and
526 figure out what happened.
527
528 Read the doc for your particular Inline Language Support Module for
529 more information.
530
531 The 'config' Registry File
532
533 Inline keeps a cached file of all of the Inline Language Support Mod‐
534 ule's meta data in a file called "config". This file can be found in
535 your "DIRECTORY" directory. If the file does not exist, Inline creates
536 a new one. It will search your system for any module beginning with
537 "Inline::". It will then call that module's "register()" method to get
538 useful information for future invocations.
539
540 Whenever you add a new ILSM, you should delete this file so that Inline
541 will auto-discover your newly installed language module.
542
544 This section lists all of the generic Inline configuration options. For
545 language specific configuration, see the doc for that language.
546
547 DIRECTORY
548
549 The "DIRECTORY" config option is the directory that Inline uses to both
550 build and install an extension.
551
552 Normally Inline will search in a bunch of known places for a directory
553 called '.Inline/'. Failing that, it will create a directory called
554 '_Inline/'
555
556 If you want to specify your own directory, use this configuration
557 option.
558
559 Note that you must create the "DIRECTORY" directory yourself. Inline
560 will not do it for you.
561
562 NAME
563
564 You can use this option to set the name of your Inline extension object
565 module. For example:
566
567 use Inline C => 'DATA',
568 NAME => 'Foo::Bar';
569
570 would cause your C code to be compiled in to the object:
571
572 lib/auto/Foo/Bar/Bar.so
573 lib/auto/Foo/Bar/Bar.inl
574
575 (The .inl component contains dependency information to make sure the
576 source code is in sync with the executable)
577
578 If you don't use NAME, Inline will pick a name for you based on your
579 program name or package name. In this case, Inline will also enable the
580 AUTONAME option which mangles in a small piece of the MD5 fingerprint
581 into your object name, to make it unique.
582
583 AUTONAME
584
585 This option is enabled whenever the NAME parameter is not specified. To
586 disable it say:
587
588 use Inline C => 'DATA',
589 DISABLE => 'AUTONAME';
590
591 AUTONAME mangles in enough of the MD5 fingerprint to make your module
592 name unique. Objects created with AUTONAME will never get replaced.
593 That also means they will never get cleaned up automatically.
594
595 AUTONAME is very useful for small throw away scripts. For more serious
596 things, always use the NAME option.
597
598 VERSION
599
600 Specifies the version number of the Inline extension object. It is used
601 only for modules, and it must match the global variable $VERSION.
602 Additionally, this option should used if (and only if) a module is
603 being set up to be installed permanently into the Perl sitelib tree.
604 Inline will croak if you use it otherwise.
605
606 The presence of the VERSION parameter is the official way to let Inline
607 know that your code is an installable/installed module. Inline will
608 never generate an object in the temporary cache (_Inline/ directory) if
609 VERSION is set. It will also never try to recompile a module that was
610 installed into someone's Perl site tree.
611
612 So the basic rule is develop without VERSION, and deliver with VERSION.
613
614 WITH
615
616 "WITH" can also be used as a configuration option instead of using the
617 special 'with' syntax. Do this if you want to use different sections of
618 Inline code with different modules. (Probably a very rare usage)
619
620 use Event;
621 use Inline C => DATA => WITH => 'Event';
622
623 Modules specified using the config form of "WITH" will not be automati‐
624 cally required. You must "use" them yourself.
625
626 GLOBAL_LOAD
627
628 This option is for compiled languages only. It tells Inline to tell
629 DynaLoader to load an object file in such a way that its symbols can be
630 dynamically resolved by other object files. May not work on all plat‐
631 forms. See the "GLOBAL" shortcut below.
632
633 UNTAINT
634
635 You must use this option whenever you use Perl's "-T" switch, for taint
636 checking. This option tells Inline to blindly untaint all tainted vari‐
637 ables. It also turns on SAFEMODE by default. See the "UNTAINT" shortcut
638 below.
639
640 SAFEMODE
641
642 Perform extra safety checking, in an attempt to thwart malicious code.
643 This option cannot guarantee security, but it does turn on all the cur‐
644 rently implemented checks.
645
646 There is a slight startup penalty by using SAFEMODE. Also, using
647 UNTAINT automatically turns this option on. If you need your code to
648 start faster under "-T" (taint) checking, you'll need to turn this
649 option off manually. Only do this if you are not worried about security
650 risks. See the "UNSAFE" shortcut below.
651
652 FORCE_BUILD
653
654 Makes Inline build (compile) the source code every time the program is
655 run. The default is 0. See the "FORCE" shortcut below.
656
657 BUILD_NOISY
658
659 Tells ILSMs that they should dump build messages to the terminal rather
660 than be silent about all the build details.
661
662 BUILD_TIMERS
663
664 Tells ILSMs to print timing information about how long each build phase
665 took. Usually requires "Time::HiRes".
666
667 CLEAN_AFTER_BUILD
668
669 Tells Inline to clean up the current build area if the build was suc‐
670 cessful. Sometimes you want to DISABLE this for debugging. Default is
671 1. See the "NOCLEAN" shortcut below.
672
673 CLEAN_BUILD_AREA
674
675 Tells Inline to clean up the old build areas within the entire Inline
676 DIRECTORY. Default is 0. See the "CLEAN" shortcut below.
677
678 PRINT_INFO
679
680 Tells Inline to print various information about the source code.
681 Default is 0. See the "INFO" shortcut below.
682
683 PRINT_VERSION
684
685 Tells Inline to print Version info about itself. Default is 0. See the
686 "VERSION" shortcut below.
687
688 REPORTBUG
689
690 Puts Inline into 'REPORTBUG' mode, which is what you want if you desire
691 to report a bug.
692
693 WARNINGS
694
695 This option tells Inline whether to print certain warnings. Default is
696 1.
697
699 This is a list of all the shorcut configuration options currently
700 available for Inline. Specify them from the command line when running
701 Inline scripts.
702
703 perl -MInline=NOCLEAN inline_script.pl
704
705 or
706
707 perl -MInline=Info,force,NoClean inline_script.pl
708
709 You can specify multiple shortcuts separated by commas. They are not
710 case sensitive. You can also specify shorcuts inside the Inline program
711 like this:
712
713 use Inline 'Info', 'Force', 'Noclean';
714
715 NOTE: If a 'use Inline' statement is used to set shortcuts, it can not
716 be used for additional purposes.
717
718 CLEAN
719 Tells Inline to remove any build directories that may be lying
720 around in your build area. Normally these directories get removed
721 immediately after a successful build. Exceptions are when the build
722 fails, or when you use the NOCLEAN or REPORTBUG options.
723
724 FORCE
725 Forces the code to be recompiled, even if everything is up to date.
726
727 GLOBAL
728 Turns on the GLOBAL_LOAD option.
729
730 INFO
731 This is a very useful option when you want to know what's going on
732 under the hood. It tells Inline to print helpful information to
733 "STDERR". Among the things that get printed is a list of which
734 Inline functions were successfully bound to Perl.
735
736 NOCLEAN
737 Tells Inline to leave the build files after compiling.
738
739 NOISY
740 Use the BUILD_NOISY option to print messages during a build.
741
742 REPORTBUG
743 Puts Inline into 'REPORTBUG' mode, which does special processing
744 when you want to report a bug. REPORTBUG also automatically forces
745 a build, and doesn't clean up afterwards. This is so that you can
746 tar and mail the build directory to me. REPORTBUG will print exact
747 instructions on what to do. Please read and follow them carefully.
748
749 NOTE: REPORTBUG informs you to use the tar command. If your system
750 does not have tar, please use the equivalent "zip" command.
751
752 SAFE
753 Turns SAFEMODE on. UNTAINT will turn this on automatically. While
754 this mode performs extra security checking, it does not guarantee
755 safety.
756
757 SITE_INSTALL
758 This parameter used to be used for creating installable Inline mod‐
759 ules. It has been removed from Inline altogether and replaced with
760 a much simpler and more powerful mechanism, "Inline::MakeMaker".
761 See the section below on how to create modules with Inline.
762
763 TIMERS
764 Turn on BUILD_TIMERS to get extra diagnostic info about builds.
765
766 UNSAFE
767 Turns SAFEMODE off. Use this in combination with UNTAINT for
768 slightly faster startup time under "-T". Only use this if you are
769 sure the environment is safe.
770
771 UNTAINT
772 Turn the UNTAINT option on. Used with "-T" switch.
773
774 VERSION
775 Tells Inline to report its release version.
776
778 Writing CPAN modules that use C code is easy with Inline. Let's say
779 that you wanted to write a module called "Math::Simple". Start by using
780 the following command:
781
782 h2xs -PAXn Math::Simple
783
784 This will generate a bunch of files that form a skeleton of what you
785 need for a distributable module. (Read the h2xs manpage to find out
786 what the options do) Next, modify the "Simple.pm" file to look like
787 this:
788
789 package Math::Simple;
790 $VERSION = '1.23';
791
792 use base 'Exporter';
793 @EXPORT_OK = qw(add subtract);
794 use strict;
795
796 use Inline C => 'DATA',
797 VERSION => '1.23',
798 NAME => 'Math::Simple';
799
800 1;
801
802 __DATA__
803
804 =pod
805
806 =cut
807
808 __C__
809 int add(int x, int y) {
810 return x + y;
811 }
812
813 int subtract(int x, int y) {
814 return x - y;
815 }
816
817 The important things to note here are that you must specify a "NAME"
818 and "VERSION" parameter. The "NAME" must match your module's package
819 name. The "VERSION" parameter must match your module's $VERSION vari‐
820 able and they must be of the form "/^\d\.\d\d$/".
821
822 NOTE: These are Inline's sanity checks to make sure you know what
823 you're doing before uploading your code to CPAN. They insure that once
824 the module has been installed on someone's system, the module would not
825 get automatically recompiled for any reason. This makes Inline based
826 modules work in exactly the same manner as XS based ones.
827
828 Finally, you need to modify the Makefile.PL. Simply change:
829
830 use ExtUtils::MakeMaker;
831
832 to
833
834 use Inline::MakeMaker;
835
836 When the person installing "Math::Simple" does a ""make"", the gener‐
837 ated Makefile will invoke Inline in such a way that the C code will be
838 compiled and the executable code will be placed into the "./blib"
839 directory. Then when a ""make install"" is done, the module will be
840 copied into the appropiate Perl sitelib directory (which is where an
841 installed module should go).
842
843 Now all you need to do is:
844
845 perl Makefile.PL
846 make dist
847
848 That will generate the file "Math-Simple-0.20.tar.gz" which is a dis‐
849 tributable package. That's all there is to it.
850
851 IMPORTANT NOTE: Although the above steps will produce a workable mod‐
852 ule, you still have a few more responsibilities as a budding new CPAN
853 author. You need to write lots of documentation and write lots of
854 tests. Take a look at some of the better CPAN modules for ideas on cre‐
855 ating a killer test harness. Actually, don't listen to me, go read
856 these:
857
858 perldoc perlnewmod
859 http://www.cpan.org/modules/04pause.html
860 http://www.cpan.org/modules/00modlist.long.html
861
863 In reality, Inline just automates everything you would need to do if
864 you were going to do it by hand (using XS, etc).
865
866 Inline performs the following steps:
867
868 1) Receive the Source Code
869 Inline gets the source code from your script or module with a
870 statements like the following:
871
872 use Inline C => "Source-Code";
873
874 or
875
876 use Inline;
877 bind Inline C => "Source-Code";
878
879 where "C" is the programming language of the source code, and
880 "Source-Code" is a string, a file name, an array reference, or the
881 special 'DATA' keyword.
882
883 Since Inline is coded in a ""use"" statement, everything is done
884 during Perl's compile time. If anything needs to be done that will
885 affect the "Source-Code", it needs to be done in a "BEGIN" block
886 that is before the ""use Inline ..."" statement. If you really need
887 to specify code to Inline at runtime, you can use the "bind()"
888 method.
889
890 Source code that is stowed in the 'DATA' section of your code, is
891 read in by an "INIT" subroutine in Inline. That's because the
892 "DATA" filehandle is not available at compile time.
893
894 2) Check if the Source Code has been Built
895 Inline only needs to build the source code if it has not yet been
896 built. It accomplishes this seemingly magical task in an extremely
897 simple and straightforward manner. It runs the source text through
898 the "Digest::MD5" module to produce a 128-bit "fingerprint" which
899 is virtually unique. The fingerprint along with a bunch of other
900 contingency information is stored in a ".inl" file that sits next
901 to your executable object. For instance, the "C" code from a script
902 called "example.pl" might create these files:
903
904 example_pl_3a9a.so
905 example_pl_3a9a.inl
906
907 If all the contingency information matches the values stored in the
908 ".inl" file, then proceed to step 8. (No compilation is necessary)
909
910 3) Find a Place to Build and Install
911 At this point Inline knows it needs to build the source code. The
912 first thing to figure out is where to create the great big mess
913 associated with compilation, and where to put the object when it's
914 done.
915
916 By default Inline will try to build and install under the first
917 place that meets one of the following conditions:
918
919 A) The DIRECTORY= config option; if specified
920 B) The PERL_INLINE_DIRECTORY environment variable; if set
921 C) .Inline/ (in current directory); if exists and $PWD != $HOME
922 D) bin/.Inline/ (in directory of your script); if exists
923 E) ~/.Inline/; if exists
924 F) ./_Inline/; if exists
925 G) bin/_Inline; if exists
926 H) Create ./_Inline/; if possible
927 I) Create bin/_Inline/; if possible
928
929 Failing that, Inline will croak. This is rare and easily remedied
930 by just making a directory that Inline will use;
931
932 If the module option is being compiled for permanent installation,
933 then Inline will only use "./_Inline/" to build in, and the $Con‐
934 fig{installsitearch} directory to install the executable in. This
935 action is caused by Inline::MakeMaker, and is intended to be used
936 in modules that are to be distributed on the CPAN, so that they get
937 installed in the proper place.
938
939 4) Parse the Source for Semantic Cues
940 Inline::C uses the module "Parse::RecDescent" to parse through your
941 chunks of C source code and look for things that it can create run-
942 time bindings to. In "C" it looks for all of the function defini‐
943 tions and breaks them down into names and data types. These ele‐
944 ments are used to correctly bind the "C" function to a "Perl" sub‐
945 routine. Other Inline languages like Python and Java actually use
946 the "python" and "javac" modules to parse the Inline code.
947
948 5) Create the Build Environment
949 Now Inline can take all of the gathered information and create an
950 environment to build your source code into an executable. Without
951 going into all the details, it just creates the appropriate direc‐
952 tories, creates the appropriate source files including an XS file
953 (for C) and a "Makefile.PL".
954
955 6) Build the Code and Install the Executable
956 The planets are in alignment. Now for the easy part. Inline just
957 does what you would do to install a module. ""perl Makefile.PL &&
958 make && make test && make install"". If something goes awry, Inline
959 will croak with a message indicating where to look for more info.
960
961 7) Tidy Up
962 By default, Inline will remove all of the mess created by the build
963 process, assuming that everything worked. If the build fails,
964 Inline will leave everything intact, so that you can debug your
965 errors. Setting the "NOCLEAN" shortcut option will also stop Inline
966 from cleaning up.
967
968 8) DynaLoad the Executable
969 For C (and C++), Inline uses the "DynaLoader::bootstrap" method to
970 pull your external module into "Perl" space. Now you can call all
971 of your external functions like Perl subroutines.
972
973 Other languages like Python and Java, provide their own loaders.
974
976 For information about using Inline with C see Inline::C.
977
978 For sample programs using Inline with C see Inline::C-Cookbook.
979
980 For "Formerly Answered Questions" about Inline, see Inline-FAQ.
981
982 For information on supported languages and platforms see Inline-Sup‐
983 port.
984
985 For information on writing your own Inline Language Support Module, see
986 Inline-API.
987
988 Inline's mailing list is inline@perl.org
989
990 To subscribe, send email to inline-subscribe@perl.org
991
993 When reporting a bug, please do the following:
994
995 - Put "use Inline REPORTBUG;" at the top of your code, or
996 use the command line option "perl -MInline=REPORTBUG ...".
997 - Run your code.
998 - Follow the printed directions.
999
1001 Brian Ingerson <INGY@cpan.org>
1002
1003 Neil Watkiss <NEILW@cpan.org> is the author of "Inline::CPP",
1004 "Inline::Python", "Inline::Ruby", "Inline::ASM", "Inline::Struct" and
1005 "Inline::Filters". He is known in the innermost Inline circles as the
1006 "Boy Wonder".
1007
1009 Copyright (c) 2000, 2001, 2002. Brian Ingerson. All rights reserved.
1010
1011 This program is free software; you can redistribute it and/or modify it
1012 under the same terms as Perl itself.
1013
1014 See http://www.perl.com/perl/misc/Artistic.html
1015
1016
1017
1018perl v5.8.8 2002-10-28 Inline(3)