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