xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/ext/B/O.pm (revision 0:68f95e015346)
1*0Sstevel@tonic-gatepackage O;
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateour $VERSION = '1.00';
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gateuse B qw(minus_c save_BEGINs);
6*0Sstevel@tonic-gateuse Carp;
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gatesub import {
9*0Sstevel@tonic-gate    my ($class, @options) = @_;
10*0Sstevel@tonic-gate    my ($quiet, $veryquiet) = (0, 0);
11*0Sstevel@tonic-gate    if ($options[0] eq '-q' || $options[0] eq '-qq') {
12*0Sstevel@tonic-gate	$quiet = 1;
13*0Sstevel@tonic-gate	open (SAVEOUT, ">&STDOUT");
14*0Sstevel@tonic-gate	close STDOUT;
15*0Sstevel@tonic-gate	open (STDOUT, ">", \$O::BEGIN_output);
16*0Sstevel@tonic-gate	if ($options[0] eq '-qq') {
17*0Sstevel@tonic-gate	    $veryquiet = 1;
18*0Sstevel@tonic-gate	}
19*0Sstevel@tonic-gate	shift @options;
20*0Sstevel@tonic-gate    }
21*0Sstevel@tonic-gate    my $backend = shift (@options);
22*0Sstevel@tonic-gate    eval q[
23*0Sstevel@tonic-gate	BEGIN {
24*0Sstevel@tonic-gate	    minus_c;
25*0Sstevel@tonic-gate	    save_BEGINs;
26*0Sstevel@tonic-gate	}
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate	CHECK {
29*0Sstevel@tonic-gate	    if ($quiet) {
30*0Sstevel@tonic-gate		close STDOUT;
31*0Sstevel@tonic-gate		open (STDOUT, ">&SAVEOUT");
32*0Sstevel@tonic-gate		close SAVEOUT;
33*0Sstevel@tonic-gate	    }
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate	    # Note: if you change the code after this 'use', please
36*0Sstevel@tonic-gate	    # change the fudge factors in B::Concise (grep for
37*0Sstevel@tonic-gate	    # "fragile kludge") so that its output still looks
38*0Sstevel@tonic-gate	    # nice. Thanks. --smcc
39*0Sstevel@tonic-gate	    use B::].$backend.q[ ();
40*0Sstevel@tonic-gate	    if ($@) {
41*0Sstevel@tonic-gate		croak "use of backend $backend failed: $@";
42*0Sstevel@tonic-gate	    }
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate	    my $compilesub = &{"B::${backend}::compile"}(@options);
46*0Sstevel@tonic-gate	    if (ref($compilesub) ne "CODE") {
47*0Sstevel@tonic-gate		die $compilesub;
48*0Sstevel@tonic-gate	    }
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate	    local $savebackslash = $\;
51*0Sstevel@tonic-gate	    local ($\,$",$,) = (undef,' ','');
52*0Sstevel@tonic-gate	    &$compilesub();
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate	    close STDERR if $veryquiet;
55*0Sstevel@tonic-gate	}
56*0Sstevel@tonic-gate    ];
57*0Sstevel@tonic-gate    die $@ if $@;
58*0Sstevel@tonic-gate}
59*0Sstevel@tonic-gate
60*0Sstevel@tonic-gate1;
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate__END__
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate=head1 NAME
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gateO - Generic interface to Perl Compiler backends
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate=head1 SYNOPSIS
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate	perl -MO=[-q,]Backend[,OPTIONS] foo.pl
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate=head1 DESCRIPTION
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gateThis is the module that is used as a frontend to the Perl Compiler.
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gateIf you pass the C<-q> option to the module, then the STDOUT
77*0Sstevel@tonic-gatefilehandle will be redirected into the variable C<$O::BEGIN_output>
78*0Sstevel@tonic-gateduring compilation.  This has the effect that any output printed
79*0Sstevel@tonic-gateto STDOUT by BEGIN blocks or use'd modules will be stored in this
80*0Sstevel@tonic-gatevariable rather than printed. It's useful with those backends which
81*0Sstevel@tonic-gateproduce output themselves (C<Deparse>, C<Concise> etc), so that
82*0Sstevel@tonic-gatetheir output is not confused with that generated by the code
83*0Sstevel@tonic-gatebeing compiled.
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gateThe C<-qq> option behaves like C<-q>, except that it also closes
86*0Sstevel@tonic-gateSTDERR after deparsing has finished. This suppresses the "Syntax OK"
87*0Sstevel@tonic-gatemessage normally produced by perl.
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate=head1 CONVENTIONS
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gateMost compiler backends use the following conventions: OPTIONS
92*0Sstevel@tonic-gateconsists of a comma-separated list of words (no white-space).
93*0Sstevel@tonic-gateThe C<-v> option usually puts the backend into verbose mode.
94*0Sstevel@tonic-gateThe C<-ofile> option generates output to B<file> instead of
95*0Sstevel@tonic-gatestdout. The C<-D> option followed by various letters turns on
96*0Sstevel@tonic-gatevarious internal debugging flags. See the documentation for the
97*0Sstevel@tonic-gatedesired backend (named C<B::Backend> for the example above) to
98*0Sstevel@tonic-gatefind out about that backend.
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate=head1 IMPLEMENTATION
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gateThis section is only necessary for those who want to write a
103*0Sstevel@tonic-gatecompiler backend module that can be used via this module.
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gateThe command-line mentioned in the SYNOPSIS section corresponds to
106*0Sstevel@tonic-gatethe Perl code
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate    use O ("Backend", OPTIONS);
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gateThe C<import> function which that calls loads in the appropriate
111*0Sstevel@tonic-gateC<B::Backend> module and calls the C<compile> function in that
112*0Sstevel@tonic-gatepackage, passing it OPTIONS. That function is expected to return
113*0Sstevel@tonic-gatea sub reference which we'll call CALLBACK. Next, the "compile-only"
114*0Sstevel@tonic-gateflag is switched on (equivalent to the command-line option C<-c>)
115*0Sstevel@tonic-gateand a CHECK block is registered which calls CALLBACK. Thus the main
116*0Sstevel@tonic-gatePerl program mentioned on the command-line is read in, parsed and
117*0Sstevel@tonic-gatecompiled into internal syntax tree form. Since the C<-c> flag is
118*0Sstevel@tonic-gateset, the program does not start running (excepting BEGIN blocks of
119*0Sstevel@tonic-gatecourse) but the CALLBACK function registered by the compiler
120*0Sstevel@tonic-gatebackend is called.
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gateIn summary, a compiler backend module should be called "B::Foo"
123*0Sstevel@tonic-gatefor some foo and live in the appropriate directory for that name.
124*0Sstevel@tonic-gateIt should define a function called C<compile>. When the user types
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate    perl -MO=Foo,OPTIONS foo.pl
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gatethat function is called and is passed those OPTIONS (split on
129*0Sstevel@tonic-gatecommas). It should return a sub ref to the main compilation function.
130*0Sstevel@tonic-gateAfter the user's program is loaded and parsed, that returned sub ref
131*0Sstevel@tonic-gateis invoked which can then go ahead and do the compilation, usually by
132*0Sstevel@tonic-gatemaking use of the C<B> module's functionality.
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate=head1 BUGS
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gateThe C<-q> and C<-qq> options don't work correctly if perl isn't
137*0Sstevel@tonic-gatecompiled with PerlIO support : STDOUT will be closed instead of being
138*0Sstevel@tonic-gateredirected to C<$O::BEGIN_output>.
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate=head1 AUTHOR
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gateMalcolm Beattie, C<mbeattie@sable.ox.ac.uk>
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate=cut
145