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