[DOC PATCH] Pluggable runops
[p5sagit/p5-mst-13.2.git] / ext / B / O.pm
CommitLineData
a798dbf2 1package O;
059a8bb7 2use B qw(minus_c save_BEGINs);
a798dbf2 3use Carp;
4
5sub 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
501;
51
7f20e9dd 52__END__
53
54=head1 NAME
55
56O - 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 64This is the module that is used as a frontend to the Perl Compiler.
65
34a48b4b 66If you pass the C<-q> option to the module, then the STDOUT
67filehandle will be redirected into the variable C<$O::BEGIN_output>
68during compilation. This has the effect that any output printed
69to STDOUT by BEGIN blocks or use'd modules will be stored in this
70variable rather than printed. It's useful with those backends which
71produce output themselves (C<Deparse>, C<Concise> etc), so that
72their output is not confused with that generated by the code
73being compiled.
74
485988ae 75The C<-qq> option behaves like C<-q>, except that it also closes
76STDERR after deparsing has finished. This suppresses the "Syntax OK"
77message normally produced by perl.
78
1a52ab62 79=head1 CONVENTIONS
80
81Most compiler backends use the following conventions: OPTIONS
82consists of a comma-separated list of words (no white-space).
83The C<-v> option usually puts the backend into verbose mode.
84The C<-ofile> option generates output to B<file> instead of
85stdout. The C<-D> option followed by various letters turns on
86various internal debugging flags. See the documentation for the
87desired backend (named C<B::Backend> for the example above) to
88find out about that backend.
89
90=head1 IMPLEMENTATION
91
92This section is only necessary for those who want to write a
93compiler backend module that can be used via this module.
94
95The command-line mentioned in the SYNOPSIS section corresponds to
96the Perl code
97
98 use O ("Backend", OPTIONS);
99
100The C<import> function which that calls loads in the appropriate
101C<B::Backend> module and calls the C<compile> function in that
102package, passing it OPTIONS. That function is expected to return
103a sub reference which we'll call CALLBACK. Next, the "compile-only"
104flag is switched on (equivalent to the command-line option C<-c>)
7d30b5c4 105and a CHECK block is registered which calls CALLBACK. Thus the main
1a52ab62 106Perl program mentioned on the command-line is read in, parsed and
107compiled into internal syntax tree form. Since the C<-c> flag is
108set, the program does not start running (excepting BEGIN blocks of
109course) but the CALLBACK function registered by the compiler
110backend is called.
111
112In summary, a compiler backend module should be called "B::Foo"
113for some foo and live in the appropriate directory for that name.
114It should define a function called C<compile>. When the user types
115
116 perl -MO=Foo,OPTIONS foo.pl
117
118that function is called and is passed those OPTIONS (split on
119commas). It should return a sub ref to the main compilation function.
120After the user's program is loaded and parsed, that returned sub ref
121is invoked which can then go ahead and do the compilation, usually by
122making use of the C<B> module's functionality.
7f20e9dd 123
124=head1 AUTHOR
125
126Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
127
128=cut