5 use B qw(minus_c save_BEGINs);
9 my ($class, @options) = @_;
10 my ($quiet, $veryquiet) = (0, 0);
11 if ($options[0] eq '-q' || $options[0] eq '-qq') {
13 open (SAVEOUT, ">&STDOUT");
15 open (STDOUT, ">", \$O::BEGIN_output);
16 if ($options[0] eq '-qq') {
21 my $backend = shift (@options);
31 open (STDOUT, ">&SAVEOUT");
34 use B::].$backend.q[ ();
36 croak "use of backend $backend failed: $@";
40 my $compilesub = &{"B::${backend}::compile"}(@options);
41 if (ref($compilesub) ne "CODE") {
45 local $savebackslash = $\;
46 local ($\,$",$,) = (undef,' ','');
49 close STDERR if $veryquiet;
61 O - Generic interface to Perl Compiler backends
65 perl -MO=[-q,]Backend[,OPTIONS] foo.pl
69 This is the module that is used as a frontend to the Perl Compiler.
71 If you pass the C<-q> option to the module, then the STDOUT
72 filehandle will be redirected into the variable C<$O::BEGIN_output>
73 during compilation. This has the effect that any output printed
74 to STDOUT by BEGIN blocks or use'd modules will be stored in this
75 variable rather than printed. It's useful with those backends which
76 produce output themselves (C<Deparse>, C<Concise> etc), so that
77 their output is not confused with that generated by the code
80 The C<-qq> option behaves like C<-q>, except that it also closes
81 STDERR after deparsing has finished. This suppresses the "Syntax OK"
82 message normally produced by perl.
86 Most compiler backends use the following conventions: OPTIONS
87 consists of a comma-separated list of words (no white-space).
88 The C<-v> option usually puts the backend into verbose mode.
89 The C<-ofile> option generates output to B<file> instead of
90 stdout. The C<-D> option followed by various letters turns on
91 various internal debugging flags. See the documentation for the
92 desired backend (named C<B::Backend> for the example above) to
93 find out about that backend.
97 This section is only necessary for those who want to write a
98 compiler backend module that can be used via this module.
100 The command-line mentioned in the SYNOPSIS section corresponds to
103 use O ("Backend", OPTIONS);
105 The C<import> function which that calls loads in the appropriate
106 C<B::Backend> module and calls the C<compile> function in that
107 package, passing it OPTIONS. That function is expected to return
108 a sub reference which we'll call CALLBACK. Next, the "compile-only"
109 flag is switched on (equivalent to the command-line option C<-c>)
110 and a CHECK block is registered which calls CALLBACK. Thus the main
111 Perl program mentioned on the command-line is read in, parsed and
112 compiled into internal syntax tree form. Since the C<-c> flag is
113 set, the program does not start running (excepting BEGIN blocks of
114 course) but the CALLBACK function registered by the compiler
117 In summary, a compiler backend module should be called "B::Foo"
118 for some foo and live in the appropriate directory for that name.
119 It should define a function called C<compile>. When the user types
121 perl -MO=Foo,OPTIONS foo.pl
123 that function is called and is passed those OPTIONS (split on
124 commas). It should return a sub ref to the main compilation function.
125 After the user's program is loaded and parsed, that returned sub ref
126 is invoked which can then go ahead and do the compilation, usually by
127 making use of the C<B> module's functionality.
131 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>