Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Inline-API.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "Inline-API 3"
132.TH Inline-API 3 "2002-10-28" "perl v5.8.0" "User Contributed Perl Documentation"
133.SH "NAME"
134Inline\-API \- How to bind a programming language to Perl using Inline.pm
135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS"
137.Vb 1
138\& #!/usr/bin/perl
139.Ve
140.PP
141.Vb 2
142\& use Inline Foo;
143\& say_it('foo'); # Use Foo to print "Hello, Foo"
144.Ve
145.PP
146.Vb 5
147\& __Foo__
148\& foo-sub say_it {
149\& foo-my $foo = foo-shift;
150\& foo-print "Hello, $foo\en";
151\& }
152.Ve
153.SH "DESCRIPTION"
154.IX Header "DESCRIPTION"
155So you think Inline C is pretty cool, but what you really need is for
156Perl to work with the brand new programming language \*(L"Foo\*(R". Well you're
157in luck. \f(CW\*(C`Inline.pm\*(C'\fR has support for adding your own Inline Language
158Support Module (\fB\s-1ILSM\s0\fR), like \f(CW\*(C`Inline::Foo\*(C'\fR.
159.PP
160Inline has always been intended to work with lots of different
161programming languages. Many of the details can be shared between
162implementations, so that \f(CW\*(C`Inline::Java\*(C'\fR has a similar interface to
163\&\f(CW\*(C`Inline::ASM\*(C'\fR. All of the common code is in \f(CW\*(C`Inline.pm\*(C'\fR.
164.PP
165Language specific modules like \f(CW\*(C`Inline::Python\*(C'\fR are subclasses of
166\&\f(CW\*(C`Inline.pm\*(C'\fR. They can inherit as much of the common behaviour as they
167want, and provide specific behaviour of their own. This usually comes in
168the form of Configuration Options and language specific compilation.
169.PP
170The Inline C support is probably the best boilerplate to copy from.
171Since version 0.30 all C support was isolated into the module
172\&\f(CW\*(C`Inline::C\*(C'\fR and the parsing grammar is further broken out into
173\&\f(CW\*(C`Inline::C::grammar\*(C'\fR. All of these components come with the Inline
174distribution.
175.PP
176This \s-1POD\s0 gives you all the details you need for implementing an \s-1ILSM\s0.
177For further assistance, contact inline@perl.org See \*(L"\s-1SEE\s0 \s-1ALSO\s0\*(R" below.
178.PP
179We'll examine the joke language Inline::Foo which is distributed with
180Inline. It actually is a full functioning \s-1ILSM\s0. I use it in Inline's
181test harness to test base Inline functionality. It is very short, and
182can help you get your head wrapped around the Inline \s-1API\s0.
183.SH "A Skeleton"
184.IX Header "A Skeleton"
185For the remainder of this tutorial, let's assume we're writing an \s-1ILSM\s0
186for the ficticious language \f(CW\*(C`Foo\*(C'\fR. We'll call it \f(CW\*(C`Inline::Foo\*(C'\fR. Here
187is the entire (working) implementation.
188.PP
189.Vb 6
190\& package Inline::Foo;
191\& use strict;
192\& $Inline::Foo::VERSION = '0.01';
193\& @Inline::Foo::ISA = qw(Inline);
194\& require Inline;
195\& use Carp;
196.Ve
197.PP
198.Vb 11
199\& #===========================================================
200\& # Register Foo as an Inline Language Support Module (ILSM)
201\& #===========================================================
202\& sub register {
203\& return {
204\& language => 'Foo',
205\& aliases => ['foo'],
206\& type => 'interpreted',
207\& suffix => 'foo',
208\& };
209\& }
210.Ve
211.PP
212.Vb 7
213\& #===========================================================
214\& # Error messages
215\& #===========================================================
216\& sub usage_config {
217\& my ($key) = @_;
218\& "'$key' is not a valid config option for Inline::Foo\en";
219\& }
220.Ve
221.PP
222.Vb 3
223\& sub usage_config_bar {
224\& "Invalid value for Inline::Foo config option BAR";
225\& }
226.Ve
227.PP
228.Vb 22
229\& #===========================================================
230\& # Validate the Foo Config Options
231\& #===========================================================
232\& sub validate {
233\& my $o = shift;
234\& $o->{ILSM}{PATTERN} ||= 'foo-';
235\& $o->{ILSM}{BAR} ||= 0;
236\& while (@_) {
237\& my ($key, $value) = splice @_, 0, 2;
238\& if ($key eq 'PATTERN') {
239\& $o->{ILSM}{PATTERN} = $value;
240\& next;
241\& }
242\& if ($key eq 'BAR') {
243\& croak usage_config_bar
244\& unless $value =~ /^[01]$/;
245\& $o->{ILSM}{BAR} = $value;
246\& next;
247\& }
248\& croak usage_config($key);
249\& }
250\& }
251.Ve
252.PP
253.Vb 23
254\& #===========================================================
255\& # Parse and compile Foo code
256\& #===========================================================
257\& sub build {
258\& my $o = shift;
259\& my $code = $o->{API}{code};
260\& my $pattern = $o->{ILSM}{PATTERN};
261\& $code =~ s/$pattern//g;
262\& $code =~ s/bar-//g if $o->{ILSM}{BAR};
263\& sleep 1; # imitate compile delay
264\& {
265\& package Foo::Tester;
266\& eval $code;
267\& }
268\& croak "Foo build failed:\en$@" if $@;
269\& my $path = "$o->{API}{install_lib}/auto/$o->{API}{modpname}";
270\& my $obj = $o->{API}{location};
271\& $o->mkpath($path) unless -d $path;
272\& open FOO_OBJ, "> $obj"
273\& or croak "Can't open $obj for output\en$!";
274\& print FOO_OBJ $code;
275\& close \e*FOO_OBJ;
276\& }
277.Ve
278.PP
279.Vb 13
280\& #===========================================================
281\& # Only needed for interpreted languages
282\& #===========================================================
283\& sub load {
284\& my $o = shift;
285\& my $obj = $o->{API}{location};
286\& open FOO_OBJ, "< $obj"
287\& or croak "Can't open $obj for output\en$!";
288\& my $code = join '', <FOO_OBJ>;
289\& close \e*FOO_OBJ;
290\& eval "package $o->{API}{pkg};\en$code";
291\& croak "Unable to load Foo module $obj:\en$@" if $@;
292\& }
293.Ve
294.PP
295.Vb 13
296\& #===========================================================
297\& # Return a small report about the Foo code.
298\& #===========================================================
299\& sub info {
300\& my $o = shift;
301\& my $text = <<'END';
302\& This is a small report about the Foo code. Perhaps it contains
303\& information about the functions the parser found which will be
304\& bound to Perl. It will get included in the text produced by the
305\& Inline 'INFO' command.
306\& END
307\& return $text;
308\& }
309.Ve
310.PP
311.Vb 1
312\& 1;
313.Ve
314.PP
315Except for \f(CW\*(C`load()\*(C'\fR, the subroutines in this code are mandatory for an
316\&\s-1ILSM\s0. What they do is described below. A few things to note:
317.IP "1" 4
318.IX Item "1"
319\&\f(CW\*(C`Inline::Foo\*(C'\fR must be a subclass of Inline. This is accomplished with:
320.Sp
321.Vb 1
322\& @Inline::Foo::ISA = qw(Inline);
323.Ve
324.IP "2" 4
325.IX Item "2"
326The line '\f(CW\*(C`require Inline;\*(C'\fR' is not necessary. But it is there to
327remind you not to say '\f(CW\*(C`use Inline;\*(C'\fR'. This will not work.
328.IP "3" 4
329.IX Item "3"
330Remember, it is not valid for a user to say:
331.Sp
332.Vb 1
333\& use Inline::Foo;
334.Ve
335.Sp
336\&\f(CW\*(C`Inline.pm\*(C'\fR will detect such usage for you in its \f(CW\*(C`import\*(C'\fR method,
337which is automatically inherited since \f(CW\*(C`Inline::Foo\*(C'\fR is a subclass.
338.IP "4" 4
339.IX Item "4"
340In the build function, you normally need to parse your source code.
341Inline::C uses Parse::RecDescent to do this. Inline::Foo simply uses
342eval. (After we strip out all occurances of 'foo\-').
343.Sp
344An alternative parsing method that works well for many ILSMs (like Java
345and Python) is to use the language's compiler itself to parse for you.
346This works as long as the compiler can be made to give back parse
347information.
348.SH "The Inline API"
349.IX Header "The Inline API"
350This section is a more formal specification of what functionality you'll
351need to provide to implement an \s-1ILSM\s0.
352.PP
353When Inline determines that some \f(CW\*(C`Foo\*(C'\fR code needs to be compiled it
354will automatically load your \s-1ILSM\s0 module. It will then call various
355subroutines which you need to supply. We'll call these subroutines
356\&\*(L"callbacks\*(R".
357.PP
358You will need to provide the following 5 callback subroutines.
359.Sh "The \fIregister()\fP Callback"
360.IX Subsection "The register() Callback"
361This subroutine receives no arguments. It returns a reference to a hash
362of \s-1ILSM\s0 meta\-data. Inline calls this routine only when it is trying to
363detect new ILSM-s that have been installed on a given system. Here is an
364example of the has ref you would return for Foo:
365.PP
366.Vb 6
367\& {
368\& language => 'Foo',
369\& aliases => ['foo'],
370\& type => 'interpreted',
371\& suffix => 'foo',
372\& };
373.Ve
374.PP
375The meta-data items have the following meanings:
376.IP "language" 4
377.IX Item "language"
378This is the proper name of the language. It is usually implemented as
379\&\f(CW\*(C`Inline::X\*(C'\fR for a given language 'X'.
380.IP "aliases" 4
381.IX Item "aliases"
382This is a reference to an array of language name aliases. The proper
383name of a language can only contain word characters. [A\-Za\-z0\-9_] An
384alias can contain any characters except whitespace and quotes. This is
385useful for names like '\*(C+' and 'C#'.
386.IP "type" 4
387.IX Item "type"
388Must be set to 'compiled' or 'interpreted'. Indicates the category of
389the language.
390.IP "suffix" 4
391.IX Item "suffix"
392This is the file extension for the cached object that will be created.
393For 'compiled' languages, it will probably be 'so' or 'dll'. The
394appropriate value is in \f(CW\*(C`Config.pm\*(C'\fR.
395.Sp
396For interpreted languages, this value can be whatever you want. Python
397uses \f(CW\*(C`pydat\*(C'\fR. Foo uses \f(CW\*(C`foo\*(C'\fR.
398.Sh "The \fIvalidate()\fP Callback"
399.IX Subsection "The validate() Callback"
400This routine gets passed all configuration options that were not already
401handled by the base Inline module. The options are passed as key/value
402pairs. It is up to you to validate each option and store its value in
403the Inline object (which is also passed in). If a particular option is
404invalid, you should croak with an appropriate error message.
405.Sh "The \fIbuild()\fP Callback"
406.IX Subsection "The build() Callback"
407This subroutine is responsible for doing the parsing and compilation of
408the Foo source code. The Inline object is passed as the only argument.
409All pertinent information will be stored in this object. \f(CW\*(C`build()\*(C'\fR is
410required to create a cache object of a specific name, or to croak with
411an appropriate error message.
412.PP
413This is the meat of your \s-1ILSM\s0. Since it will most likely be quite
414complicated, it is probably best that you study an existing \s-1ILSM\s0 like
415\&\f(CW\*(C`Inline::C\*(C'\fR.
416.Sh "The \fIload()\fP Callback"
417.IX Subsection "The load() Callback"
418This method only needs to be provided for interpreted languages. It's
419responsibility is to start the interpreter.
420.PP
421For compiled languages, the load routine from \f(CW\*(C`Inline.pm\*(C'\fR is called
422which uses \f(CW\*(C`DynaLoader\*(C'\fR to load the shared object or \s-1DLL\s0.
423.Sh "The \fIinfo()\fP Callback"
424.IX Subsection "The info() Callback"
425This method is called when the user makes use of the \f(CW\*(C`INFO\*(C'\fR
426shortcut. You should return a string containing a small report about
427the Inlined code.
428.SH "The Inline Object"
429.IX Header "The Inline Object"
430\&\f(CW\*(C`Inline.pm\*(C'\fR creates a hash based Perl object for each section of
431Inlined source code it receives. This object contains lots of
432information about the code, the environment, and the configuration
433options used.
434.PP
435This object is a hash that is broken into several subhashes. The only
436two subhashes that an \s-1ILSM\s0 should use at all are \f(CW$o\fR\->{\s-1API\s0} and
437\&\f(CW$o\fR\->{\s-1ILSM\s0}. The first one contains all of the information that Inline
438has gather for you in order for you to create/load a cached object of
439your design. The second one is a repository where your \s-1ILSM\s0 can freely
440store data that it might need later on.
441.PP
442This section will describe all of the Inline object \*(L"\s-1API\s0\*(R" attributes.
443.Sh "The code Attribute"
444.IX Subsection "The code Attribute"
445This the actual source code passed in by the user. It is stored as one
446long string.
447.Sh "The language Attribute"
448.IX Subsection "The language Attribute"
449The proper name of the language being used.
450.Sh "The language_id Attribute"
451.IX Subsection "The language_id Attribute"
452The language name specified by the user. Could be '\*(C+' instead of '\s-1CPP\s0'.
453.Sh "The module Attribute"
454.IX Subsection "The module Attribute"
455This is the shared object's file name.
456.Sh "The modfname Attribute"
457.IX Subsection "The modfname Attribute"
458This is the shared object's file name.
459.Sh "The modpname Attribute"
460.IX Subsection "The modpname Attribute"
461This is the shared object's installation path extension.
462.Sh "The version Attribute"
463.IX Subsection "The version Attribute"
464The version of \f(CW\*(C`Inline.pm\*(C'\fR being used.
465.Sh "The pkg Attribute"
466.IX Subsection "The pkg Attribute"
467The Perl package from which this invocation pf Inline was called.
468.Sh "The install_lib Attribute"
469.IX Subsection "The install_lib Attribute"
470This is the directory to write the shared object into.
471.Sh "The build_dir Attribute"
472.IX Subsection "The build_dir Attribute"
473This is the directory under which you should write all of your build
474related files.
475.Sh "The script Attribute"
476.IX Subsection "The script Attribute"
477This is the name of the script that invoked Inline.
478.Sh "The location Attribute"
479.IX Subsection "The location Attribute"
480This is the full path name of the executable object in question.
481.Sh "The suffix Attribute"
482.IX Subsection "The suffix Attribute"
483This is the shared library extension name. (Usually 'so' or 'dll').
484.SH "The Inline Namespace"
485.IX Header "The Inline Namespace"
486\&\f(CW\*(C`Inline.pm\*(C'\fR has been set up so that anyone can write their own language
487support modules. It further allows anyone to write a different
488implementation of an existing Inline language, like C for instance. You
489can distribute that module on the \s-1CPAN\s0.
490.PP
491If you have plans to implement and distribute an Inline module, I would
492ask that you please work with the Inline community. We can be reached at
493the Inline mailing list: inline@perl.org (Send mail to
494inline\-subscribe@perl.org to subscribe). Here you should find the advice
495and assistance needed to make your module a success.
496.PP
497The Inline community will decide if your implementation of \s-1COBOL\s0 will be
498distributed as the official \f(CW\*(C`Inline::COBOL\*(C'\fR or should use an alternate
499namespace. In matters of dispute, I (Brian Ingerson) retain final
500authority. (and I hope not to need use of it :\-) Actually
501modules@perl.org retains the \fBfinal\fR authority.
502.PP
503But even if you want to work alone, you are free and welcome to write
504and distribute Inline language support modules on \s-1CPAN\s0. You'll just need
505to distribute them under a different package name.
506.SH "SEE ALSO"
507.IX Header "SEE ALSO"
508For generic information about Inline, see Inline.
509.PP
510For information about using Inline with C see Inline::C.
511.PP
512For information on supported languages and platforms see
513Inline-Support.
514.PP
515Inline's mailing list is inline@perl.org
516.PP
517To subscribe, send email to inline\-subscribe@perl.org
518.SH "AUTHOR"
519.IX Header "AUTHOR"
520Brian Ingerson <INGY@cpan.org>
521.SH "COPYRIGHT"
522.IX Header "COPYRIGHT"
523Copyright (c) 2000, 2001, 2002. Brian Ingerson. All rights reserved.
524.PP
525This program is free software; you can redistribute it and/or modify it
526under the same terms as Perl itself.
527.PP
528See http://www.perl.com/perl/misc/Artistic.html