📄 o.pm
字号:
package O;our $VERSION = '1.00';use B qw(minus_c save_BEGINs);use Carp;sub import { my ($class, @options) = @_; my ($quiet, $veryquiet) = (0, 0); if ($options[0] eq '-q' || $options[0] eq '-qq') { $quiet = 1; open (SAVEOUT, ">&STDOUT"); close STDOUT; open (STDOUT, ">", \$O::BEGIN_output); if ($options[0] eq '-qq') { $veryquiet = 1; } shift @options; } my $backend = shift (@options); eval q[ BEGIN { minus_c; save_BEGINs; } CHECK { if ($quiet) { close STDOUT; open (STDOUT, ">&SAVEOUT"); close SAVEOUT; } # Note: if you change the code after this 'use', please # change the fudge factors in B::Concise (grep for # "fragile kludge") so that its output still looks # nice. Thanks. --smcc use B::].$backend.q[ (); if ($@) { croak "use of backend $backend failed: $@"; } my $compilesub = &{"B::${backend}::compile"}(@options); if (ref($compilesub) ne "CODE") { die $compilesub; } local $savebackslash = $\; local ($\,$",$,) = (undef,' ',''); &$compilesub(); close STDERR if $veryquiet; } ]; die $@ if $@;}1;__END__=head1 NAMEO - Generic interface to Perl Compiler backends=head1 SYNOPSIS perl -MO=[-q,]Backend[,OPTIONS] foo.pl=head1 DESCRIPTIONThis is the module that is used as a frontend to the Perl Compiler.If you pass the C<-q> option to the module, then the STDOUTfilehandle will be redirected into the variable C<$O::BEGIN_output>during compilation. This has the effect that any output printedto STDOUT by BEGIN blocks or use'd modules will be stored in thisvariable rather than printed. It's useful with those backends whichproduce output themselves (C<Deparse>, C<Concise> etc), so thattheir output is not confused with that generated by the codebeing compiled.The C<-qq> option behaves like C<-q>, except that it also closesSTDERR after deparsing has finished. This suppresses the "Syntax OK"message normally produced by perl.=head1 CONVENTIONSMost compiler backends use the following conventions: OPTIONSconsists of a comma-separated list of words (no white-space).The C<-v> option usually puts the backend into verbose mode.The C<-ofile> option generates output to B<file> instead ofstdout. The C<-D> option followed by various letters turns onvarious internal debugging flags. See the documentation for thedesired backend (named C<B::Backend> for the example above) tofind out about that backend.=head1 IMPLEMENTATIONThis section is only necessary for those who want to write acompiler backend module that can be used via this module.The command-line mentioned in the SYNOPSIS section corresponds tothe Perl code use O ("Backend", OPTIONS);The C<O::import> function loads the appropriate C<B::Backend> moduleand calls its C<compile> function, passing it OPTIONS. That functionis expected to return a sub reference which we'll call CALLBACK. Next,the "compile-only" flag is switched on (equivalent to the command-lineoption C<-c>) and a CHECK block is registered which callsCALLBACK. Thus the main Perl program mentioned on the command-line isread in, parsed and compiled into internal syntax tree form. Since theC<-c> flag is set, the program does not start running (excepting BEGINblocks of course) but the CALLBACK function registered by the compilerbackend is called.In summary, a compiler backend module should be called "B::Foo"for some foo and live in the appropriate directory for that name.It should define a function called C<compile>. When the user types perl -MO=Foo,OPTIONS foo.plthat function is called and is passed those OPTIONS (split oncommas). It should return a sub ref to the main compilation function.After the user's program is loaded and parsed, that returned sub refis invoked which can then go ahead and do the compilation, usually bymaking use of the C<B> module's functionality.=head1 BUGSThe C<-q> and C<-qq> options don't work correctly if perl isn'tcompiled with PerlIO support : STDOUT will be closed instead of beingredirected to C<$O::BEGIN_output>.=head1 AUTHORMalcolm Beattie, C<mbeattie@sable.ox.ac.uk>=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -