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