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