b::deparse.3

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 3 代码 · 共 614 行 · 第 1/2 页

3
614
字号
If you're using B::Deparse on a module or other file that's require'd,you shouldn't use a value that evaluates to false, since the customarytrue constant at the end of a module will be in void context when thefile is compiled as a main program..RE.RS 4.RE.IP "\fB\-x\fR\fI\s-1LEVEL\s0\fR" 4.IX Item "-xLEVEL"Expand conventional syntax constructions into equivalent ones that exposetheir internal operation. \fI\s-1LEVEL\s0\fR should be a digit, with higher valuesmeaning more expansion. As with \fB\-q\fR, this actually involves turning offspecial cases in B::Deparse's normal operations..SpIf \fI\s-1LEVEL\s0\fR is at least 3, \f(CW\*(C`for\*(C'\fR loops will be translated into equivalentwhile loops with continue blocks; for instance.Sp.Vb 3\&    for ($i = 0; $i < 10; ++$i) {\&        print $i;\&    }.Ve.Spturns into.Sp.Vb 6\&    $i = 0;\&    while ($i < 10) {\&        print $i;\&    } continue {\&        ++$i\&    }.Ve.SpNote that in a few cases this translation can't be perfectly carried backinto the source code \*(-- if the loop's initializer declares a my variable,for instance, it won't have the correct scope outside of the loop..SpIf \fI\s-1LEVEL\s0\fR is at least 5, \f(CW\*(C`use\*(C'\fR declarations will be translated into\&\f(CW\*(C`BEGIN\*(C'\fR blocks containing calls to \f(CW\*(C`require\*(C'\fR and \f(CW\*(C`import\*(C'\fR; forinstance,.Sp.Vb 1\&    use strict \*(Aqrefs\*(Aq;.Ve.Spturns into.Sp.Vb 6\&    sub BEGIN {\&        require strict;\&        do {\&            \*(Aqstrict\*(Aq\->import(\*(Aqrefs\*(Aq)\&        };\&    }.Ve.SpIf \fI\s-1LEVEL\s0\fR is at least 7, \f(CW\*(C`if\*(C'\fR statements will be translated intoequivalent expressions using \f(CW\*(C`&&\*(C'\fR, \f(CW\*(C`?:\*(C'\fR and \f(CW\*(C`do {}\*(C'\fR; for instance.Sp.Vb 9\&    print \*(Aqhi\*(Aq if $nice;\&    if ($nice) {\&        print \*(Aqhi\*(Aq;\&    }\&    if ($nice) {\&        print \*(Aqhi\*(Aq;\&    } else {\&        print \*(Aqbye\*(Aq;\&    }.Ve.Spturns into.Sp.Vb 3\&    $nice and print \*(Aqhi\*(Aq;\&    $nice and do { print \*(Aqhi\*(Aq };\&    $nice ? do { print \*(Aqhi\*(Aq } : do { print \*(Aqbye\*(Aq };.Ve.SpLong sequences of elsifs will turn into nested ternary operators, whichB::Deparse doesn't know how to indent nicely..SH "USING B::Deparse AS A MODULE".IX Header "USING B::Deparse AS A MODULE".Sh "Synopsis".IX Subsection "Synopsis".Vb 4\&    use B::Deparse;\&    $deparse = B::Deparse\->new("\-p", "\-sC");\&    $body = $deparse\->coderef2text(\e&func);\&    eval "sub func $body"; # the inverse operation.Ve.Sh "Description".IX Subsection "Description"B::Deparse can also be used on a sub-by-sub basis from other perlprograms..Sh "new".IX Subsection "new".Vb 1\&    $deparse = B::Deparse\->new(OPTIONS).Ve.PPCreate an object to store the state of a deparsing operation and anyoptions. The options are the same as those that can be given on thecommand line (see \*(L"\s-1OPTIONS\s0\*(R"); options that are separated by commasafter \fB\-MO=Deparse\fR should be given as separate strings. Someoptions, like \fB\-u\fR, don't make sense for a single subroutine, sodon't pass them..Sh "ambient_pragmas".IX Subsection "ambient_pragmas".Vb 1\&    $deparse\->ambient_pragmas(strict => \*(Aqall\*(Aq, \*(Aq$[\*(Aq => $[);.Ve.PPThe compilation of a subroutine can be affected by a few compilerdirectives, \fBpragmas\fR. These are:.IP "\(bu" 4use strict;.IP "\(bu" 4use warnings;.IP "\(bu" 4Assigning to the special variable $[.IP "\(bu" 4use integer;.IP "\(bu" 4use bytes;.IP "\(bu" 4use utf8;.IP "\(bu" 4use re;.PPOrdinarily, if you use B::Deparse on a subroutine which hasbeen compiled in the presence of one or more of these pragmas,the output will include statements to turn on the appropriatedirectives. So if you then compile the code returned by coderef2text,it will behave the same way as the subroutine which you deparsed..PPHowever, you may know that you intend to use the results in aparticular context, where some pragmas are already in scope. Inthis case, you use the \fBambient_pragmas\fR method to describe theassumptions you wish to make..PPNot all of the options currently have any useful effect. See\&\*(L"\s-1BUGS\s0\*(R" for more details..PPThe parameters it accepts are:.IP "strict" 4.IX Item "strict"Takes a string, possibly containing several values separatedby whitespace. The special values \*(L"all\*(R" and \*(L"none\*(R" mean what you'dexpect..Sp.Vb 1\&    $deparse\->ambient_pragmas(strict => \*(Aqsubs refs\*(Aq);.Ve.IP "$[" 4Takes a number, the value of the array base $[..IP "bytes" 4.IX Item "bytes".PD 0.IP "utf8" 4.IX Item "utf8".IP "integer" 4.IX Item "integer".PDIf the value is true, then the appropriate pragma is assumed tobe in the ambient scope, otherwise not..IP "re" 4.IX Item "re"Takes a string, possibly containing a whitespace-separated list ofvalues. The values \*(L"all\*(R" and \*(L"none\*(R" are special. It's also permissibleto pass an array reference here..Sp.Vb 1\&    $deparser\->ambient_pragmas(re => \*(Aqeval\*(Aq);.Ve.IP "warnings" 4.IX Item "warnings"Takes a string, possibly containing a whitespace-separated list ofvalues. The values \*(L"all\*(R" and \*(L"none\*(R" are special, again. It's alsopermissible to pass an array reference here..Sp.Vb 1\&    $deparser\->ambient_pragmas(warnings => [qw[void io]]);.Ve.SpIf one of the values is the string \*(L"\s-1FATAL\s0\*(R", then all the warningsin that list will be considered fatal, just as with the \fBwarnings\fRpragma itself. Should you need to specify that some warnings arefatal, and others are merely enabled, you can pass the \fBwarnings\fRparameter twice:.Sp.Vb 4\&    $deparser\->ambient_pragmas(\&        warnings => \*(Aqall\*(Aq,\&        warnings => [FATAL => qw/void io/],\&    );.Ve.SpSee perllexwarn for more information about lexical warnings..IP "hint_bits" 4.IX Item "hint_bits".PD 0.IP "warning_bits" 4.IX Item "warning_bits".PDThese two parameters are used to specify the ambient pragmas inthe format used by the special variables $^H and ${^WARNING_BITS}..SpThey exist principally so that you can write code like:.Sp.Vb 7\&    { my ($hint_bits, $warning_bits);\&    BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}\&    $deparser\->ambient_pragmas (\&        hint_bits    => $hint_bits,\&        warning_bits => $warning_bits,\&        \*(Aq$[\*(Aq         => 0 + $[\&    ); }.Ve.Spwhich specifies that the ambient pragmas are exactly those whichare in scope at the point of calling..IP "%^H" 4.IX Item "%^H"This parameter is used to specify the ambient pragmas which arestored in the special hash %^H..Sh "coderef2text".IX Subsection "coderef2text".Vb 2\&    $body = $deparse\->coderef2text(\e&func)\&    $body = $deparse\->coderef2text(sub ($$) { ... }).Ve.PPReturn source code for the body of a subroutine (a block, optionallypreceded by a prototype in parens), given a reference to thesub. Because a subroutine can have no names, or more than one name,this method doesn't return a complete subroutine definition \*(-- if youwant to eval the result, you should prepend \*(L"sub subname \*(R", or \*(L"sub \*(R"for an anonymous function constructor. Unless the sub was defined inthe main:: package, the code will include a package declaration..SH "BUGS".IX Header "BUGS".IP "\(bu" 4The only pragmas to be completely supported are: \f(CW\*(C`use warnings\*(C'\fR,\&\f(CW\*(C`use strict \*(Aqrefs\*(Aq\*(C'\fR, \f(CW\*(C`use bytes\*(C'\fR, and \f(CW\*(C`use integer\*(C'\fR. (\f(CW$[\fR, whichbehaves like a pragma, is also supported.).SpExcepting those listed above, we're currently unable to guarantee thatB::Deparse will produce a pragma at the correct point in the program.(Specifically, pragmas at the beginning of a block often appear rightbefore the start of the block instead.)Since the effects of pragmas are often lexically scoped, this can meanthat the pragma holds sway over a different portion of the programthan in the input file..IP "\(bu" 4In fact, the above is a specific instance of a more general problem:we can't guarantee to produce \s-1BEGIN\s0 blocks or \f(CW\*(C`use\*(C'\fR declarations inexactly the right place. So if you use a module which affects compilation(such as by over-riding keywords, overloading constants or whatever)then the output code might not work as intended..SpThis is the most serious outstanding problem, and will require some helpfrom the Perl core to fix..IP "\(bu" 4If a keyword is over-ridden, and your program explicitly callsthe built-in version by using CORE::keyword, the output of B::Deparsewill not reflect this. If you run the resulting code, it will callthe over-ridden version rather than the built-in one. (Maybe thereshould be an option to \fBalways\fR print keyword calls as \f(CW\*(C`CORE::name\*(C'\fR.).IP "\(bu" 4Some constants don't print correctly either with or without \fB\-d\fR.For instance, neither B::Deparse nor Data::Dumper know how to printdual-valued scalars correctly, as in:.Sp.Vb 1\&    use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;.Ve.IP "\(bu" 4An input file that uses source filtering probably won't be deparsed intorunnable code, because it will still include the \fBuse\fR declarationfor the source filtering module, even though the code that isproduced is already ordinary Perl which shouldn't be filtered again..IP "\(bu" 4Optimised away statements are rendered as '???'. This includes statements thathave a compile-time side-effect, such as the obscure.Sp.Vb 1\&    my $x if 0;.Ve.Spwhich is not, consequently, deparsed correctly..IP "\(bu" 4Lexical (my) variables declared in scopes external to a subroutineappear in code2ref output text as package variables. This is a trickyproblem, as perl has no native facility for refering to a lexical variabledefined within a different scope, although PadWalker is a good start..IP "\(bu" 4There are probably many more bugs on non-ASCII platforms (\s-1EBCDIC\s0)..SH "AUTHOR".IX Header "AUTHOR"Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier versionby Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with contributions fromGisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell,Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and RafaelGarcia-Suarez.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?