Commit | Line | Data |
a798dbf2 |
1 | package O; |
059a8bb7 |
2 | use B qw(minus_c save_BEGINs); |
a798dbf2 |
3 | use Carp; |
4 | |
5 | sub import { |
34a48b4b |
6 | my ($class, @options) = @_; |
7 | my $quiet = 0; |
8 | if ($options[0] eq '-q') { |
9 | $quiet = 1; |
10 | shift @options; |
11 | open (SAVEOUT, ">&STDOUT"); |
12 | close STDOUT; |
13 | open (STDOUT, ">", \$O::BEGIN_output); |
14 | } |
15 | my $backend = shift (@options); |
7a9b44b9 |
16 | eval q[ |
17 | BEGIN { |
18 | minus_c; |
19 | save_BEGINs; |
20 | } |
21 | |
22 | CHECK { |
34a48b4b |
23 | if ($quiet) { |
24 | close STDOUT; |
25 | open (STDOUT, ">&SAVEOUT"); |
26 | close SAVEOUT; |
27 | } |
7a9b44b9 |
28 | use B::].$backend.q[ (); |
29 | if ($@) { |
30 | croak "use of backend $backend failed: $@"; |
31 | } |
32 | |
33 | |
34 | my $compilesub = &{"B::${backend}::compile"}(@options); |
35 | if (ref($compilesub) ne "CODE") { |
36 | die $compilesub; |
37 | } |
38 | |
39 | &$compilesub(); |
40 | } |
41 | ]; |
42 | die $@ if $@; |
a798dbf2 |
43 | } |
44 | |
45 | 1; |
46 | |
7f20e9dd |
47 | __END__ |
48 | |
49 | =head1 NAME |
50 | |
51 | O - Generic interface to Perl Compiler backends |
52 | |
53 | =head1 SYNOPSIS |
54 | |
34a48b4b |
55 | perl -MO=[-q,]Backend[,OPTIONS] foo.pl |
7f20e9dd |
56 | |
57 | =head1 DESCRIPTION |
58 | |
1a52ab62 |
59 | This is the module that is used as a frontend to the Perl Compiler. |
60 | |
34a48b4b |
61 | If you pass the C<-q> option to the module, then the STDOUT |
62 | filehandle will be redirected into the variable C<$O::BEGIN_output> |
63 | during compilation. This has the effect that any output printed |
64 | to STDOUT by BEGIN blocks or use'd modules will be stored in this |
65 | variable rather than printed. It's useful with those backends which |
66 | produce output themselves (C<Deparse>, C<Concise> etc), so that |
67 | their output is not confused with that generated by the code |
68 | being compiled. |
69 | |
1a52ab62 |
70 | =head1 CONVENTIONS |
71 | |
72 | Most compiler backends use the following conventions: OPTIONS |
73 | consists of a comma-separated list of words (no white-space). |
74 | The C<-v> option usually puts the backend into verbose mode. |
75 | The C<-ofile> option generates output to B<file> instead of |
76 | stdout. The C<-D> option followed by various letters turns on |
77 | various internal debugging flags. See the documentation for the |
78 | desired backend (named C<B::Backend> for the example above) to |
79 | find out about that backend. |
80 | |
81 | =head1 IMPLEMENTATION |
82 | |
83 | This section is only necessary for those who want to write a |
84 | compiler backend module that can be used via this module. |
85 | |
86 | The command-line mentioned in the SYNOPSIS section corresponds to |
87 | the Perl code |
88 | |
89 | use O ("Backend", OPTIONS); |
90 | |
91 | The C<import> function which that calls loads in the appropriate |
92 | C<B::Backend> module and calls the C<compile> function in that |
93 | package, passing it OPTIONS. That function is expected to return |
94 | a sub reference which we'll call CALLBACK. Next, the "compile-only" |
95 | flag is switched on (equivalent to the command-line option C<-c>) |
7d30b5c4 |
96 | and a CHECK block is registered which calls CALLBACK. Thus the main |
1a52ab62 |
97 | Perl program mentioned on the command-line is read in, parsed and |
98 | compiled into internal syntax tree form. Since the C<-c> flag is |
99 | set, the program does not start running (excepting BEGIN blocks of |
100 | course) but the CALLBACK function registered by the compiler |
101 | backend is called. |
102 | |
103 | In summary, a compiler backend module should be called "B::Foo" |
104 | for some foo and live in the appropriate directory for that name. |
105 | It should define a function called C<compile>. When the user types |
106 | |
107 | perl -MO=Foo,OPTIONS foo.pl |
108 | |
109 | that function is called and is passed those OPTIONS (split on |
110 | commas). It should return a sub ref to the main compilation function. |
111 | After the user's program is loaded and parsed, that returned sub ref |
112 | is invoked which can then go ahead and do the compilation, usually by |
113 | making use of the C<B> module's functionality. |
7f20e9dd |
114 | |
115 | =head1 AUTHOR |
116 | |
117 | Malcolm Beattie, C<mbeattie@sable.ox.ac.uk> |
118 | |
119 | =cut |