| 1 |
package O; |
| 2 |
|
| 3 |
our $VERSION = '1.03'; |
| 4 |
|
| 5 |
use B (); |
| 6 |
|
| 7 |
our $BEGIN_output; |
| 8 |
our $saveout_fh; |
| 9 |
|
| 10 |
sub 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 |
|
| 62 |
1; |
| 63 |
|
| 64 |
__END__ |
| 65 |
|
| 66 |
=head1 NAME |
| 67 |
|
| 68 |
O - Generic interface to Perl Compiler backends |
| 69 |
|
| 70 |
=head1 SYNOPSIS |
| 71 |
|
| 72 |
perl -MO=[-q,]Backend[,OPTIONS] foo.pl |
| 73 |
|
| 74 |
=head1 DESCRIPTION |
| 75 |
|
| 76 |
This is the module that is used as a frontend to the Perl Compiler. |
| 77 |
|
| 78 |
If you pass the C<-q> option to the module, then the STDOUT |
| 79 |
filehandle will be redirected into the variable C<$O::BEGIN_output> |
| 80 |
during compilation. This has the effect that any output printed |
| 81 |
to STDOUT by BEGIN blocks or use'd modules will be stored in this |
| 82 |
variable rather than printed. It's useful with those backends which |
| 83 |
produce output themselves (C<Deparse>, C<Concise> etc), so that |
| 84 |
their output is not confused with that generated by the code |
| 85 |
being compiled. |
| 86 |
|
| 87 |
The C<-qq> option behaves like C<-q>, except that it also closes |
| 88 |
STDERR after deparsing has finished. This suppresses the "Syntax OK" |
| 89 |
message normally produced by perl. |
| 90 |
|
| 91 |
=head1 CONVENTIONS |
| 92 |
|
| 93 |
Most compiler backends use the following conventions: OPTIONS |
| 94 |
consists of a comma-separated list of words (no white-space). |
| 95 |
The C<-v> option usually puts the backend into verbose mode. |
| 96 |
The C<-ofile> option generates output to B<file> instead of |
| 97 |
stdout. The C<-D> option followed by various letters turns on |
| 98 |
various internal debugging flags. See the documentation for the |
| 99 |
desired backend (named C<B::Backend> for the example above) to |
| 100 |
find out about that backend. |
| 101 |
|
| 102 |
=head1 IMPLEMENTATION |
| 103 |
|
| 104 |
This section is only necessary for those who want to write a |
| 105 |
compiler backend module that can be used via this module. |
| 106 |
|
| 107 |
The command-line mentioned in the SYNOPSIS section corresponds to |
| 108 |
the Perl code |
| 109 |
|
| 110 |
use O ("Backend", OPTIONS); |
| 111 |
|
| 112 |
The C<O::import> function loads the appropriate C<B::Backend> module |
| 113 |
and calls its C<compile> function, passing it OPTIONS. That function |
| 114 |
is expected to return a sub reference which we'll call CALLBACK. Next, |
| 115 |
the "compile-only" flag is switched on (equivalent to the command-line |
| 116 |
option C<-c>) and a CHECK block is registered which calls |
| 117 |
CALLBACK. Thus the main Perl program mentioned on the command-line is |
| 118 |
read in, parsed and compiled into internal syntax tree form. Since the |
| 119 |
C<-c> flag is set, the program does not start running (excepting BEGIN |
| 120 |
blocks of course) but the CALLBACK function registered by the compiler |
| 121 |
backend is called. |
| 122 |
|
| 123 |
In summary, a compiler backend module should be called "B::Foo" |
| 124 |
for some foo and live in the appropriate directory for that name. |
| 125 |
It should define a function called C<compile>. When the user types |
| 126 |
|
| 127 |
perl -MO=Foo,OPTIONS foo.pl |
| 128 |
|
| 129 |
that function is called and is passed those OPTIONS (split on |
| 130 |
commas). It should return a sub ref to the main compilation function. |
| 131 |
After the user's program is loaded and parsed, that returned sub ref |
| 132 |
is invoked which can then go ahead and do the compilation, usually by |
| 133 |
making use of the C<B> module's functionality. |
| 134 |
|
| 135 |
=head1 BUGS |
| 136 |
|
| 137 |
The C<-q> and C<-qq> options don't work correctly if perl isn't |
| 138 |
compiled with PerlIO support : STDOUT will be closed instead of being |
| 139 |
redirected to C<$O::BEGIN_output>. |
| 140 |
|
| 141 |
=head1 AUTHOR |
| 142 |
|
| 143 |
Malcolm Beattie, C<[email protected]> |
| 144 |
|
| 145 |
=cut |