2 use B qw(minus_c save_BEGINs);
6 my ($class, @options) = @_;
8 if ($options[0] eq '-q') {
11 open (SAVEOUT, ">&STDOUT");
13 open (STDOUT, ">", \$O::BEGIN_output);
15 my $backend = shift (@options);
25 open (STDOUT, ">&SAVEOUT");
28 use B::].$backend.q[ ();
30 croak "use of backend $backend failed: $@";
34 my $compilesub = &{"B::${backend}::compile"}(@options);
35 if (ref($compilesub) ne "CODE") {
51 O - Generic interface to Perl Compiler backends
55 perl -MO=[-q,]Backend[,OPTIONS] foo.pl
59 This is the module that is used as a frontend to the Perl Compiler.
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
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.
83 This section is only necessary for those who want to write a
84 compiler backend module that can be used via this module.
86 The command-line mentioned in the SYNOPSIS section corresponds to
89 use O ("Backend", OPTIONS);
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>)
96 and a CHECK block is registered which calls CALLBACK. Thus the main
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
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
107 perl -MO=Foo,OPTIONS foo.pl
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.
117 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>