📄 opcode.pm
字号:
package Opcode;use 5.006_001;use strict;our($VERSION, @ISA, @EXPORT_OK);$VERSION = "1.11";use Carp;use Exporter ();use XSLoader ();BEGIN { @ISA = qw(Exporter); @EXPORT_OK = qw( opset ops_to_opset opset_to_ops opset_to_hex invert_opset empty_opset full_opset opdesc opcodes opmask define_optag opmask_add verify_opset opdump );}sub opset (;@);sub opset_to_hex ($);sub opdump (;$);use subs @EXPORT_OK;XSLoader::load 'Opcode', $VERSION;_init_optags();sub ops_to_opset { opset @_ } # alias for old namesub opset_to_hex ($) { return "(invalid opset)" unless verify_opset($_[0]); unpack("h*",$_[0]);}sub opdump (;$) { my $pat = shift; # handy utility: perl -MOpcode=opdump -e 'opdump File' foreach(opset_to_ops(full_opset)) { my $op = sprintf " %12s %s\n", $_, opdesc($_); next if defined $pat and $op !~ m/$pat/i; print $op; }}sub _init_optags { my(%all, %seen); @all{opset_to_ops(full_opset)} = (); # keys only local($_); local($/) = "\n=cut"; # skip to optags definition section <DATA>; $/ = "\n="; # now read in 'pod section' chunks while(<DATA>) { next unless m/^item\s+(:\w+)/; my $tag = $1; # Split into lines, keep only indented lines my @lines = grep { m/^\s/ } split(/\n/); foreach (@lines) { s/--.*// } # delete comments my @ops = map { split ' ' } @lines; # get op words foreach(@ops) { warn "$tag - $_ already tagged in $seen{$_}\n" if $seen{$_}; $seen{$_} = $tag; delete $all{$_}; } # opset will croak on invalid names define_optag($tag, opset(@ops)); } close(DATA); warn "Untagged opnames: ".join(' ',keys %all)."\n" if %all;}1;__DATA__=head1 NAMEOpcode - Disable named opcodes when compiling perl code=head1 SYNOPSIS use Opcode;=head1 DESCRIPTIONPerl code is always compiled into an internal format before execution.Evaluating perl code (e.g. via "eval" or "do 'file'") causesthe code to be compiled into an internal format and then,provided there was no error in the compilation, executed.The internal format is based on many distinct I<opcodes>.By default no opmask is in effect and any code can be compiled.The Opcode module allow you to define an I<operator mask> to be ineffect when perl I<next> compiles any code. Attempting to compile codewhich contains a masked opcode will cause the compilation to failwith an error. The code will not be executed.=head1 NOTEThe Opcode module is not usually used directly. See the ops pragma andSafe modules for more typical uses.=head1 WARNINGThe authors make B<no warranty>, implied or otherwise, about thesuitability of this software for safety or security purposes.The authors shall not in any case be liable for special, incidental,consequential, indirect or other similar damages arising from the useof this software.Your mileage will vary. If in any doubt B<do not use it>.=head1 Operator Names and Operator ListsThe canonical list of operator names is the contents of the arrayPL_op_name defined and initialised in file F<opcode.h> of the Perlsource distribution (and installed into the perl library).Each operator has both a terse name (its opname) and a more verbose orrecognisable descriptive name. The opdesc function can be used toreturn a list of descriptions for a list of operators.Many of the functions and methods listed below take a list ofoperators as parameters. Most operator lists can be made up of severaltypes of element. Each element can be one of=over 8=item an operator name (opname)Operator names are typically small lowercase words like enterloop,leaveloop, last, next, redo etc. Sometimes they are rather crypticlike gv2cv, i_ncmp and ftsvtx.=item an operator tag name (optag)Operator tags can be used to refer to groups (or sets) of operators.Tag names always begin with a colon. The Opcode module defines severaloptags and the user can define others using the define_optag function.=item a negated opname or optagAn opname or optag can be prefixed with an exclamation mark, e.g., !mkdir.Negating an opname or optag means remove the corresponding ops from theaccumulated set of ops at that point.=item an operator set (opset)An I<opset> as a binary string of approximately 44 bytes which holds aset or zero or more operators.The opset and opset_to_ops functions can be used to convert froma list of operators to an opset and I<vice versa>.Wherever a list of operators can be given you can use one or more opsets.See also Manipulating Opsets below.=back=head1 Opcode FunctionsThe Opcode package contains functions for manipulating operator namestags and sets. All are available for export by the package.=over 8=item opcodesIn a scalar context opcodes returns the number of opcodes in thisversion of perl (around 350 for perl-5.7.0).In a list context it returns a list of all the operator names.(Not yet implemented, use @names = opset_to_ops(full_opset).)=item opset (OP, ...)Returns an opset containing the listed operators.=item opset_to_ops (OPSET)Returns a list of operator names corresponding to those operators inthe set.=item opset_to_hex (OPSET)Returns a string representation of an opset. Can be handy for debugging.=item full_opsetReturns an opset which includes all operators.=item empty_opsetReturns an opset which contains no operators.=item invert_opset (OPSET)Returns an opset which is the inverse set of the one supplied.=item verify_opset (OPSET, ...)Returns true if the supplied opset looks like a valid opset (is theright length etc) otherwise it returns false. If an optional secondparameter is true then verify_opset will croak on an invalid opsetinstead of returning false.Most of the other Opcode functions call verify_opset automaticallyand will croak if given an invalid opset.=item define_optag (OPTAG, OPSET)Define OPTAG as a symbolic name for OPSET. Optag names always startwith a colon C<:>.The optag name used must not be defined already (define_optag willcroak if it is already defined). Optag names are global to the perlprocess and optag definitions cannot be altered or deleted oncedefined.It is strongly recommended that applications using Opcode should use aleading capital letter on their tag names since lowercase names arereserved for use by the Opcode module. If using Opcode within a moduleyou should prefix your tags names with the name of your module toensure uniqueness and thus avoid clashes with other modules.=item opmask_add (OPSET)Adds the supplied opset to the current opmask. Note that there iscurrently I<no> mechanism for unmasking ops once they have been masked.This is intentional.=item opmaskReturns an opset corresponding to the current opmask.=item opdesc (OP, ...)This takes a list of operator names and returns the corresponding listof operator descriptions.=item opdump (PAT)Dumps to STDOUT a two column list of op names and op descriptions.If an optional pattern is given then only lines which match the(case insensitive) pattern will be output.It's designed to be used as a handy command line utility: perl -MOpcode=opdump -e opdump perl -MOpcode=opdump -e 'opdump Eval'=back=head1 Manipulating OpsetsOpsets may be manipulated using the perl bit vector operators & (and), | (or),^ (xor) and ~ (negate/invert).However you should never rely on the numerical position of any opcodewithin the opset. In other words both sides of a bit vector operatorshould be opsets returned from Opcode functions.Also, since the number of opcodes in your current version of perl mightnot be an exact multiple of eight, there may be unused bits in the lastbyte of an upset. This should not cause any problems (Opcode functionsignore those extra bits) but it does mean that using the ~ operatorwill typically not produce the same 'physical' opset 'string' as theinvert_opset function.=head1 TO DO (maybe) $bool = opset_eq($opset1, $opset2) true if opsets are logically eqiv $yes = opset_can($opset, @ops) true if $opset has all @ops set @diff = opset_diff($opset1, $opset2) => ('foo', '!bar', ...)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -