Seth Woolley's Man Viewer

B::Concise(3) - B::Concise - Walk Perl syntax tree, printing concise info about ops - man 3 B::Concise

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

B::Concise(3)          Perl Programmers Reference Guide          B::Concise(3)



NAME
       B::Concise - Walk Perl syntax tree, printing concise info(1,5,n) about ops

SYNOPSIS
           perl -MO=Concise[,OPTIONS] foo.pl

           use B::Concise qw(set_style add_callback);

DESCRIPTION
       This compiler backend prints the internal OPs of a Perl program's syn-
       tax tree in(1,8) one of several space-efficient text formats suitable for
       debugging the inner workings of perl or other compiler backends. It can
       print OPs in(1,8) the order they appear in(1,8) the OP tree, in(1,8) the order they
       will execute, or in(1,8) a text approximation to their tree structure, and
       the format of the information displyed is customizable. Its function is
       similar to that of perl's -Dx debugging flag or the B::Terse module,
       but it is more sophisticated and flexible.

EXAMPLE
       Here's is a short example of output (aka 'rendering'), using the
       default formatting conventions :

           % perl -MO=Concise -e '$a = $b + 42'
           8  <@> leave[1 ref] vKP/REFC ->(end)
           1     <0> enter ->2
           2     <;> nextstate(main 1 -e:1) v ->3
           7     <2> sassign vKS/2 ->8
           5        <2> add[t1] sK/2 ->6
           -           <1> ex-rv2sv sK/1 ->4
           3              <$> gvsv(*b) s ->4
           4           <$> const(IV 42) s ->5
           -        <1> ex-rv2sv sKRM*/1 ->7
           6           <$> gvsv(*a) s ->7

       Each line corresponds to an opcode. Null ops appear as "ex-opname",
       where opname is the op that has been optimized away by perl.

       The number on the first row indicates the op's sequence number. It's
       given in(1,8) base 36 by default.

       The symbol between angle brackets indicates the op's type : for exam-
       ple, <2> is a BINOP, <@> a LISTOP, etc. (see "OP class abbreviations").

       The opname may be followed by op-specific information in(1,8) parentheses
       (e.g. "gvsv(*b)"), and by targ information in(1,8) brackets (e.g.
       "leave[t1]").

       Next come the op flags. The common flags are listed below ("OP flags
       abbreviations"). The private flags follow, separated by a slash. For
       example, "vKP/REFC" means that the leave op has public flags
       OPf_WANT_VOID, OPf_KIDS, and OPf_PARENS, and the private flag OPpREF-
       COUNTED.

       Finally an arrow points to the sequence number of the next op.

OPTIONS
       Arguments that don't start with a hyphen are taken to be the names of
       subroutines to print the OPs of; if(3,n) no such functions are specified,
       the main body of the program (outside any subroutines, and not includ-
       ing use'd or require'd files) is printed. Passing "BEGIN", "CHECK",
       "INIT", or "END" will cause all of the corresponding special blocks to
       be printed.

       Options affect how things are rendered (ie printed).  They're presented
       here by their visual effect, 1st being strongest.  They're grouped
       according to how they interrelate; within each group the options are
       mutually exclusive (unless otherwise stated).

       Options for Opcode Ordering

       These options control the 'vertical display' of opcodes.  The display
       'order' is also called 'mode' elsewhere in(1,8) this document.

       -basic
           Print OPs in(1,8) the order they appear in(1,8) the OP tree (a preorder tra-
           versal, starting at the root). The indentation of each OP shows its
           level in(1,8) the tree.  This mode is the default, so the flag is
           included simply for completeness.

       -exec
           Print OPs in(1,8) the order they would normally execute (for the major-
           ity of constructs this is a postorder traversal of the tree, ending
           at the root). In most cases the OP that usually follows a given OP
           will appear directly below it; alternate paths are shown by inden-
           tation. In cases like loops when control jumps out of a linear
           path, a 'goto' line is generated.

       -tree
           Print OPs in(1,8) a text approximation of a tree, with the root of the
           tree at the left and 'left-to-right' order of children transformed
           into 'top-to-bottom'. Because this mode grows both to the right and
           down, it isn't suitable for large programs (unless you have a very
           wide terminal).

       Options for Line-Style

       These options select(2,7,2 select_tut) the line-style (or just style) used to render each
       opcode, and dictates what info(1,5,n) is actually printed into each line.

       -concise
           Use the author's favorite set(7,n,1 builtins) of formatting conventions. This is
           the default, of course.

       -terse
           Use formatting conventions that emulate the output of B::Terse. The
           basic mode is almost indistinguishable from the real B::Terse, and
           the exec(3,n,1 builtins) mode looks very similar, but is in(1,8) a more logical order
           and lacks curly brackets. B::Terse doesn't have a tree mode, so the
           tree mode is only vaguely reminiscent of B::Terse.

       -linenoise
           Use formatting conventions in(1,8) which the name of each OP, rather
           than being written out in(1,8) full, is represented by a one- or two-
           character abbreviation.  This is mainly a joke.

       -debug
           Use formatting conventions reminiscent of B::Debug; these aren't
           very concise at all.

       -env
           Use formatting conventions read(2,n,1 builtins) from the environment variables
           "B_CONCISE_FORMAT", "B_CONCISE_GOTO_FORMAT", and "B_CON-
           CISE_TREE_FORMAT".

       Options for tree-specific formatting


       -compact
           Use a tree format in(1,8) which the minimum amount of space is used for
           the lines connecting nodes (one character in(1,8) most cases). This
           squeezes out a few precious columns of screen real estate.

       -loose
           Use a tree format that uses longer edges to separate OP nodes. This
           format tends to look(1,8,3 Search::Dict) better than the compact one, especially in(1,8)
           ASCII, and is the default.

       -vt Use tree connecting characters drawn from the VT100 line-drawing
           set.  This looks better if(3,n) your terminal supports it.

       -ascii
           Draw the tree with standard ASCII characters like "+" and "|".
           These don't look(1,8,3 Search::Dict) as clean as the VT100 characters, but they'll work
           with almost any terminal (or the horizontal scrolling mode of
           less(1,3)(1)) and are suitable for text documentation or email. This is
           the default.

       These are pairwise exclusive, i.e. compact or loose, vt or ascii.

       Options controlling sequence numbering


       -basen
           Print OP sequence numbers in(1,8) base n. If n is greater than 10, the
           digit for 11 will be 'a', and so on. If n is greater than 36, the
           digit for 37 will be 'A', and so on until 62. Values greater than
           62 are not currently supported. The default is 36.

       -bigendian
           Print sequence numbers with the most significant digit first. This
           is the usual convention for Arabic numerals, and the default.

       -littleendian
           Print seqence numbers with the least significant digit first.  This
           is obviously mutually exclusive with bigendian.

       Other options


       -main
           Include the main program in(1,8) the output, even if(3,n) subroutines were
           also specified.  This is the only option that is not sticky (see
           below)

       -banner
           B::Concise::compile normally prints a banner line identifying the
           function name, or in(1,8) case of a subref, a generic message including
           (unfortunately) the stringified coderef.  This option suppresses
           the printing of the banner.

       Option Stickiness

       If you invoke Concise more than once in(1,8) a program, you should know that
       the options are 'sticky'.  This means that the options you provide in(1,8)
       the first call will be remembered for the 2nd call, unless you re-spec-
       ify or change them.

FORMATTING SPECIFICATIONS
       For each line-style ('concise', 'terse', 'linenoise', etc.) there are 3
       format-specs which control how OPs are rendered.

       The first is the 'default' format, which is used in(1,8) both basic and exec(3,n,1 builtins)
       modes to print all opcodes.  The 2nd, goto-format, is used in(1,8) exec(3,n,1 builtins) mode
       when branches are encountered.  They're not real opcodes, and are
       inserted to look(1,8,3 Search::Dict) like a closing curly brace.  The tree-format is tree
       specific.

       When a line is rendered, the correct format string(3,n) is scanned for the
       following items, and data is substituted in(1,8), or other manipulations,
       like basic indenting.  Any text that doesn't match a special pattern
       (the items below) is copied verbatim.  (Yes, it's a set(7,n,1 builtins) of s///g
       steps.)

       (x(exec_text;basic_text)x)
           Generates exec_text in(1,8) exec(3,n,1 builtins) mode, or basic_text in(1,8) basic mode.

       (*(text)*)
           Generates one copy of text for each indentation level.

       (*(text1;text2)*)
           Generates one fewer copies of text1 than the indentation level,
           followed by one copy of text2 if(3,n) the indentation level is more than
           0.

       (?(text1#varText2)?)
           If the value of var is true (not empty or zero), generates the
           value of var surrounded by text1 and Text2, otherwise nothing.

       #var
           Generates the value of the variable var.

       #varN
           Generates the value of var, left jutified to fill N spaces.

       ~   Any number of tildes and surrounding whitespace will be collapsed
           to a single space.

       The following variables are recognized:

       #addr
           The address of the OP, in(1,8) hexidecimal.

       #arg
           The OP-specific information of the OP (such as the SV for an SVOP,
           the non-local exit(3,n,1 builtins) pointers for a LOOP, etc.) enclosed in(1,8) parethe-
           ses.

       #class
           The B-determined class of the OP, in(1,8) all caps.

       #classsym
           A single symbol abbreviating the class of the OP.

       #coplabel
           The label of the statement or block the OP is the start of, if(3,n) any.

       #exname
           The name of the OP, or 'ex-foo' if(3,n) the OP is a null that used to be
           a foo.

       #extarg
           The target of the OP, or nothing for a nulled OP.

       #firstaddr
           The address of the OP's first child, in(1,8) hexidecimal.

       #flags
           The OP's flags, abbreviated as a series of symbols.

       #flagval
           The numeric value of the OP's flags.

       #hyphseq
           The sequence number of the OP, or a hyphen if(3,n) it doesn't have one.

       #label
           'NEXT', 'LAST', or 'REDO' if(3,n) the OP is a target of one of those in(1,8)
           exec(3,n,1 builtins) mode, or empty otherwise.

       #lastaddr
           The address of the OP's last child, in(1,8) hexidecimal.

       #name
           The OP's name.

       #NAME
           The OP's name, in(1,8) all caps.

       #next
           The sequence number of the OP's next OP.

       #nextaddr
           The address of the OP's next OP, in(1,8) hexidecimal.

       #noise
           A one- or two-character abbreviation for the OP's name.

       #private
           The OP's private flags, rendered with abbreviated names if(3,n) possi-
           ble.

       #privval
           The numeric value of the OP's private flags.

       #seq
           The sequence number of the OP. Note that this is now a sequence
           number generated by B::Concise, rather than the real op_seq value
           (for which see #seqnum).

       #seqnum
           The real sequence number of the OP, as a regular number and not
           adjusted to be relative to the start of the real program. (This
           will generally be a fairly large number because all of B::Concise
           is compiled before your program is).

       #sibaddr
           The address of the OP's next youngest sibling, in(1,8) hexidecimal.

       #svaddr
           The address of the OP's SV, if(3,n) it has an SV, in(1,8) hexidecimal.

       #svclass
           The class of the OP's SV, if(3,n) it has one, in(1,8) all caps (e.g., 'IV').

       #svval
           The value of the OP's SV, if(3,n) it has one, in(1,8) a short human-readable
           format.

       #targ
           The numeric value of the OP's targ.

       #targarg
           The name of the variable the OP's targ refers to, if(3,n) any, otherwise
           the letter t followed by the OP's targ in(1,8) decimal.

       #targarglife
           Same as #targarg, but followed by the COP sequence numbers that
           delimit the variable's lifetime (or 'end' for a variable in(1,8) an open(2,3,n)
           scope) for a variable.

       #typenum
           The numeric value of the OP's type, in(1,8) decimal.

ABBREVIATIONS
       OP flags abbreviations

           v      OPf_WANT_VOID    Want nothing (void context)
           s      OPf_WANT_SCALAR  Want single value (scalar context)
           l      OPf_WANT_LIST    Want list of any length (list context)
           K      OPf_KIDS         There is a firstborn child.
           P      OPf_PARENS       This operator was parenthesized.
                                    (Or block needs explicit scope entry.)
           R      OPf_REF          Certified reference.
                                    (Return container, not containee).
           M      OPf_MOD          Will modify (lvalue).
           S      OPf_STACKED      Some arg is arriving on the stack.
           *      OPf_SPECIAL      Do something weird for this op (see op.h)

       OP class abbreviations

           0      OP (aka BASEOP)  An OP with no children
           1      UNOP             An OP with one child
           2      BINOP            An OP with two children
           |      LOGOP            A control branch OP
           @      LISTOP           An OP that could have lots of children
           /      PMOP             An OP with a regular expression
           $      SVOP             An OP with an SV
           "      PVOP             An OP with a string(3,n)
           {      LOOP             An OP that holds pointers for a loop
           ;      COP              An OP that marks the start of a statement
           #      PADOP            An OP with a GV on the pad

Using B::Concise outside of the O framework
       You can use B::Concise, and call compile() directly, and repeatedly.
       By doing so, you can avoid the compile-time only operation of 'perl
       -MO=Concise ..'.  For example, you can use the debugger to step through
       B::Concise::compile() itself.

       When doing so, you can alter Concise output by providing new output
       styles, and optionally by adding callback routines which populate new
       variables that may be rendered as part of those styles.  For all fol-
       lowing sections, please review "FORMATTING SPECIFICATIONS".

       Example: Altering Concise Renderings

           use B::Concise qw(set_style add_callback);
           set_style($your_format, $your_gotofmt, $your_treefmt);
           add_callback
             ( sub {
                   my ($h, $op, $format, $level, $stylename) = @_;
                   $h->{variable} = some_func($op);
               }
             );
           B::Concise::compile(@options)->();

       set_style()

       set_style accepts 3 arguments, and updates the three format-specs com-
       prising a line-style (basic-exec, goto, tree).  It has one minor draw-
       back though; it doesn't register the style under a new name.  This can
       become an issue if(3,n) you render more than once and switch(1,n) styles.  Thus
       you may prefer to use add_style() and/or set_style_standard() instead.

       set_style_standard($name)

       This restores one of the standard line-styles: "terse", "concise",
       "linenoise", "debug", "env(1,3)", into effect.  It also accepts style names
       previously defined with add_style().

       add_style()

       This subroutine accepts a new style name and three style arguments as
       above, and creates, registers, and selects the newly named(5,8) style.  It
       is an error(8,n) to re-add a style; call set_style_standard() to switch(1,n)
       between several styles.

       add_callback()

       If your newly minted styles refer to any #variables, you'll need to
       define a callback subroutine that will populate (or modify) those vari-
       ables.  They are then available for use in(1,8) the style you've chosen.

       The callbacks are called for each opcode visited by Concise, in(1,8) the
       same order as they are added.  Each subroutine is passed five parame-
       ters.

         1. A hashref, containing the variable names and values which are
            populated into the report-line for the op
         2. the op, as a B<B::OP> object
         3. a reference to the format string(3,n)
         4. the formatting (indent) level
         5. the selected stylename

       To define your own variables, simply add them to the hash, or change
       existing values if(3,n) you need to.  The level and format are passed in(1,8) as
       references to scalars, but it is unlikely that they will need to be
       changed or even used.

       Running B::Concise::compile()

       compile accepts options as described above in(1,8) "OPTIONS", and arguments,
       which are either coderefs, or subroutine names.

       compile() constructs and returns a coderef, which when invoked, scans
       the optree, and prints the results to STDOUT.  Once you have the
       coderef, you may change the output style; thereafter the coderef ren-
       ders in(1,8) the new style.

       walk_output lets you change the print destination from STDOUT to
       another open(2,3,n) filehandle, or into a string(3,n) passed as a ref.

           walk_output(\my $buf);
           my $walker = B::Concise::compile('-concise','funcName', \&aSubRef);
           print "Concise Banner for Functions: $buf\n";
           $walker->();
           print "Concise Rendering(s)?: $buf\n";

       For each subroutine visited by Concise, the $buf will contain a banner
       naming the function or coderef about to be traversed.  Once $walker is
       invoked, it prints the actual renderings for each.

       To switch(1,n) back to one of the standard styles like "concise" or "terse",
       call "set_style_standard", or pass the style name into B::Concise::com-
       pile() (as done above).

       B::Concise::reset_sequence()

       This function (not exported) lets you reset(1,7,1 tput) the sequence numbers (note
       that they're numbered arbitrarily, their goal being to be human read-
       able).  Its purpose is mostly to support testing, i.e. to compare the
       concise output from two identical anonymous subroutines (but different
       instances).  Without the reset(1,7,1 tput), B::Concise, seeing that they're sepa-
       rate optrees, generates different sequence numbers in(1,8) the output.

       Errors

       All detected errors, (invalid arguments, internal errors, etc.) are
       resolved with a die($message). Use an eval if(3,n) you wish to catch these
       errors and continue processing.

       In particular, compile will die if(3,n) you've asked for a non-existent
       function-name, a non-existent coderef, or a non-CODE reference.

AUTHOR
       Stephen McCamant, <smcc@CSUA.Berkeley.EDU>.



perl v5.8.5                       2001-09-21                     B::Concise(3)

References for this manual (incoming links)