1Inline-API(3)         User Contributed Perl Documentation        Inline-API(3)
2
3
4

NAME

6       Inline-API - How to bind a programming language to Perl using Inline.pm
7

SYNOPSIS

9           #!/usr/bin/perl
10
11           use Inline Foo;
12           say_it('foo');  # Use Foo to print "Hello, Foo"
13
14           __Foo__
15           foo-sub say_it {
16               foo-my $foo = foo-shift;
17               foo-print "Hello, $foo\n";
18           }
19

DESCRIPTION

21       So you think Inline C is pretty cool, but what you really need is for
22       Perl to work with the brand new programming language "Foo". Well you're
23       in luck. "Inline.pm" has support for adding your own Inline Language
24       Support Module (ILSM), like "Inline::Foo".
25
26       Inline has always been intended to work with lots of different program‐
27       ming languages. Many of the details can be shared between implementa‐
28       tions, so that "Inline::Java" has a similar interface to "Inline::ASM".
29       All of the common code is in "Inline.pm".
30
31       Language specific modules like "Inline::Python" are subclasses of
32       "Inline.pm". They can inherit as much of the common behaviour as they
33       want, and provide specific behaviour of their own. This usually comes
34       in the form of Configuration Options and language specific compilation.
35
36       The Inline C support is probably the best boilerplate to copy from.
37       Since version 0.30 all C support was isolated into the module
38       "Inline::C" and the parsing grammar is further broken out into
39       "Inline::C::grammar". All of these components come with the Inline dis‐
40       tribution.
41
42       This POD gives you all the details you need for implementing an ILSM.
43       For further assistance, contact inline@perl.org See "SEE ALSO" below.
44
45       We'll examine the joke language Inline::Foo which is distributed with
46       Inline. It actually is a full functioning ILSM. I use it in Inline's
47       test harness to test base Inline functionality. It is very short, and
48       can help you get your head wrapped around the Inline API.
49

A Skeleton

51       For the remainder of this tutorial, let's assume we're writing an ILSM
52       for the ficticious language "Foo". We'll call it "Inline::Foo". Here is
53       the entire (working) implementation.
54
55           package Inline::Foo;
56           use strict;
57           $Inline::Foo::VERSION = '0.01';
58           @Inline::Foo::ISA = qw(Inline);
59           require Inline;
60           use Carp;
61
62           #===========================================================
63           # Register Foo as an Inline Language Support Module (ILSM)
64           #===========================================================
65           sub register {
66               return {
67                   language => 'Foo',
68                   aliases => ['foo'],
69                   type => 'interpreted',
70                   suffix => 'foo',
71                  };
72           }
73
74           #===========================================================
75           # Error messages
76           #===========================================================
77           sub usage_config {
78               my ($key) = @_;
79               "'$key' is not a valid config option for Inline::Foo\n";
80           }
81
82           sub usage_config_bar {
83               "Invalid value for Inline::Foo config option BAR";
84           }
85
86           #===========================================================
87           # Validate the Foo Config Options
88           #===========================================================
89           sub validate {
90               my $o = shift;
91               $o->{ILSM}{PATTERN} ⎪⎪= 'foo-';
92               $o->{ILSM}{BAR} ⎪⎪= 0;
93               while (@_) {
94               my ($key, $value) = splice @_, 0, 2;
95               if ($key eq 'PATTERN') {
96                   $o->{ILSM}{PATTERN} = $value;
97                   next;
98               }
99               if ($key eq 'BAR') {
100                   croak usage_config_bar
101                     unless $value =~ /^[01]$/;
102                   $o->{ILSM}{BAR} = $value;
103                   next;
104               }
105               croak usage_config($key);
106               }
107           }
108
109           #===========================================================
110           # Parse and compile Foo code
111           #===========================================================
112           sub build {
113               my $o = shift;
114               my $code = $o->{API}{code};
115               my $pattern = $o->{ILSM}{PATTERN};
116               $code =~ s/$pattern//g;
117               $code =~ s/bar-//g if $o->{ILSM}{BAR};
118               sleep 1;             # imitate compile delay
119               {
120                   package Foo::Tester;
121                   eval $code;
122               }
123               croak "Foo build failed:\n$@" if $@;
124               my $path = "$o->{API}{install_lib}/auto/$o->{API}{modpname}";
125               my $obj = $o->{API}{location};
126               $o->mkpath($path) unless -d $path;
127               open FOO_OBJ, "> $obj"
128                 or croak "Can't open $obj for output\n$!";
129               print FOO_OBJ $code;
130               close \*FOO_OBJ;
131           }
132
133           #===========================================================
134           # Only needed for interpreted languages
135           #===========================================================
136           sub load {
137               my $o = shift;
138               my $obj = $o->{API}{location};
139               open FOO_OBJ, "< $obj"
140                 or croak "Can't open $obj for output\n$!";
141               my $code = join '', <FOO_OBJ>;
142               close \*FOO_OBJ;
143               eval "package $o->{API}{pkg};\n$code";
144               croak "Unable to load Foo module $obj:\n$@" if $@;
145           }
146
147           #===========================================================
148           # Return a small report about the Foo code.
149           #===========================================================
150           sub info {
151               my $o = shift;
152               my $text = <<'END';
153           This is a small report about the Foo code. Perhaps it contains
154           information about the functions the parser found which will be
155           bound to Perl. It will get included in the text produced by the
156           Inline 'INFO' command.
157           END
158               return $text;
159           }
160
161           1;
162
163       Except for "load()", the subroutines in this code are mandatory for an
164       ILSM. What they do is described below. A few things to note:
165
166       1   "Inline::Foo" must be a subclass of Inline. This is accomplished
167           with:
168
169               @Inline::Foo::ISA = qw(Inline);
170
171       2   The line '"require Inline;"' is not necessary. But it is there to
172           remind you not to say '"use Inline;"'. This will not work.
173
174       3   Remember, it is not valid for a user to say:
175
176               use Inline::Foo;
177
178           "Inline.pm" will detect such usage for you in its "import" method,
179           which is automatically inherited since "Inline::Foo" is a subclass.
180
181       4   In the build function, you normally need to parse your source code.
182           Inline::C uses Parse::RecDescent to do this. Inline::Foo simply
183           uses eval. (After we strip out all occurances of 'foo-').
184
185           An alternative parsing method that works well for many ILSMs (like
186           Java and Python) is to use the language's compiler itself to parse
187           for you.  This works as long as the compiler can be made to give
188           back parse information.
189

The Inline API

191       This section is a more formal specification of what functionality
192       you'll need to provide to implement an ILSM.
193
194       When Inline determines that some "Foo" code needs to be compiled it
195       will automatically load your ILSM module. It will then call various
196       subroutines which you need to supply. We'll call these subroutines
197       "callbacks".
198
199       You will need to provide the following 5 callback subroutines.
200
201       The register() Callback
202
203       This subroutine receives no arguments. It returns a reference to a hash
204       of ILSM meta-data. Inline calls this routine only when it is trying to
205       detect new ILSM-s that have been installed on a given system. Here is
206       an example of the has ref you would return for Foo:
207
208           {
209            language => 'Foo',
210            aliases => ['foo'],
211            type => 'interpreted',
212            suffix => 'foo',
213           };
214
215       The meta-data items have the following meanings:
216
217       language
218           This is the proper name of the language. It is usually implemented
219           as "Inline::X" for a given language 'X'.
220
221       aliases
222           This is a reference to an array of language name aliases. The
223           proper name of a language can only contain word characters.
224           [A-Za-z0-9_] An alias can contain any characters except whitespace
225           and quotes. This is useful for names like 'C++' and 'C#'.
226
227       type
228           Must be set to 'compiled' or 'interpreted'. Indicates the category
229           of the language.
230
231       suffix
232           This is the file extension for the cached object that will be cre‐
233           ated.  For 'compiled' languages, it will probably be 'so' or 'dll'.
234           The appropriate value is in "Config.pm".
235
236           For interpreted languages, this value can be whatever you want.
237           Python uses "pydat". Foo uses "foo".
238
239       The validate() Callback
240
241       This routine gets passed all configuration options that were not
242       already handled by the base Inline module. The options are passed as
243       key/value pairs. It is up to you to validate each option and store its
244       value in the Inline object (which is also passed in). If a particular
245       option is invalid, you should croak with an appropriate error message.
246
247       The build() Callback
248
249       This subroutine is responsible for doing the parsing and compilation of
250       the Foo source code. The Inline object is passed as the only argument.
251       All pertinent information will be stored in this object. "build()" is
252       required to create a cache object of a specific name, or to croak with
253       an appropriate error message.
254
255       This is the meat of your ILSM. Since it will most likely be quite com‐
256       plicated, it is probably best that you study an existing ILSM like
257       "Inline::C".
258
259       The load() Callback
260
261       This method only needs to be provided for interpreted languages. It's
262       responsibility is to start the interpreter.
263
264       For compiled languages, the load routine from "Inline.pm" is called
265       which uses "DynaLoader" to load the shared object or DLL.
266
267       The info() Callback
268
269       This method is called when the user makes use of the "INFO" shortcut.
270       You should return a string containing a small report about the Inlined
271       code.
272

The Inline Object

274       "Inline.pm" creates a hash based Perl object for each section of
275       Inlined source code it receives. This object contains lots of informa‐
276       tion about the code, the environment, and the configuration options
277       used.
278
279       This object is a hash that is broken into several subhashes. The only
280       two subhashes that an ILSM should use at all are $o->{API} and
281       $o->{ILSM}. The first one contains all of the information that Inline
282       has gather for you in order for you to create/load a cached object of
283       your design. The second one is a repository where your ILSM can freely
284       store data that it might need later on.
285
286       This section will describe all of the Inline object "API" attributes.
287
288       The code Attribute
289
290       This the actual source code passed in by the user. It is stored as one
291       long string.
292
293       The language Attribute
294
295       The proper name of the language being used.
296
297       The language_id Attribute
298
299       The language name specified by the user. Could be 'C++' instead of
300       'CPP'.
301
302       The module Attribute
303
304       This is the shared object's file name.
305
306       The modfname Attribute
307
308       This is the shared object's file name.
309
310       The modpname Attribute
311
312       This is the shared object's installation path extension.
313
314       The version Attribute
315
316       The version of "Inline.pm" being used.
317
318       The pkg Attribute
319
320       The Perl package from which this invocation pf Inline was called.
321
322       The install_lib Attribute
323
324       This is the directory to write the shared object into.
325
326       The build_dir Attribute
327
328       This is the directory under which you should write all of your build
329       related files.
330
331       The script Attribute
332
333       This is the name of the script that invoked Inline.
334
335       The location Attribute
336
337       This is the full path name of the executable object in question.
338
339       The suffix Attribute
340
341       This is the shared library extension name. (Usually 'so' or 'dll').
342

The Inline Namespace

344       "Inline.pm" has been set up so that anyone can write their own language
345       support modules. It further allows anyone to write a different imple‐
346       mentation of an existing Inline language, like C for instance. You can
347       distribute that module on the CPAN.
348
349       If you have plans to implement and distribute an Inline module, I would
350       ask that you please work with the Inline community. We can be reached
351       at the Inline mailing list: inline@perl.org (Send mail to inline-sub‐
352       scribe@perl.org to subscribe). Here you should find the advice and
353       assistance needed to make your module a success.
354
355       The Inline community will decide if your implementation of COBOL will
356       be distributed as the official "Inline::COBOL" or should use an alter‐
357       nate namespace. In matters of dispute, I (Brian Ingerson) retain final
358       authority. (and I hope not to need use of it :-) Actually mod‐
359       ules@perl.org retains the final authority.
360
361       But even if you want to work alone, you are free and welcome to write
362       and distribute Inline language support modules on CPAN. You'll just
363       need to distribute them under a different package name.
364

SEE ALSO

366       For generic information about Inline, see Inline.
367
368       For information about using Inline with C see Inline::C.
369
370       For information on supported languages and platforms see Inline-Sup‐
371       port.
372
373       Inline's mailing list is inline@perl.org
374
375       To subscribe, send email to inline-subscribe@perl.org
376

AUTHOR

378       Brian Ingerson <INGY@cpan.org>
379
381       Copyright (c) 2000, 2001, 2002. Brian Ingerson. All rights reserved.
382
383       This program is free software; you can redistribute it and/or modify it
384       under the same terms as Perl itself.
385
386       See http://www.perl.com/perl/misc/Artistic.html
387
388
389
390perl v5.8.8                       2002-10-28                     Inline-API(3)
Impressum