B::Concise(3pm)
๐ฆ B::Concise(3pm) ๐ฆ
from the Perl Programmers Reference Guide โ okay bestie, this page is literally about walking Perl's syntax tree and printing concise info about ops. We're in part 1, so let's go. ๐
โจ NAME โจ
B::Concise โ walk Perl syntax tree, printing concise info about ops.
๐ธ SYNOPSIS ๐ธ
perl -MO=Concise[,OPTIONS] foo.pl
use B::Concise qw(set_style add_callback);
๐ DESCRIPTION ๐
Okay so B::Concise is a compiler backend, and what it does is print the internal OPs of a Perl program's syntax tree in one of several space-efficient text formats suitable for debugging the inner workings of perl or other compiler backends. Literally its whole job. โจ
It can print OPs in the order they appear in the OP tree, in the order they will execute, or in a text approximation to their tree structure. And the format of the information displayed? Customizable, bestie. ๐
Its function is similar to that of perl's -Dx debugging flag or the B::Terse module, but it is more sophisticated and flexible. No cap, it's giving upgraded. ๐ฆ
๐ EXAMPLE ๐
Here's two outputs (or 'renderings'), using the -exec and -basic (i.e. default) formatting conventions on the same code snippet.
% perl -MO=Concise,-exec -e '$a = $b + 42'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v
3 <#> gvsv[*b] s
4 <$> const[IV 42] s
* 5 <2> add[t3] sK/2
6 <#> gvsv[*a] s
7 <2> sassign vKS/2
8 <@> leave[1 ref] vKP/REFC
In this -exec rendering, each opcode is executed in the order shown. The add opcode, marked with '*', is discussed in more detail. ๐
The 1st column is the op's sequence number, starting at 1, and is displayed in base 36 by default. Here they're purely linear; the sequences are very helpful when looking at code with loops and branches. iykyk. ๐
The symbol between angle brackets indicates the op's type, for example; <2> is a BINOP, <@> a LISTOP, and <#> is a PADOP, which is used in threaded perls. (see "OP class abbreviations"). ๐ง
The opname, as in 'add[t1]', may be followed by op-specific information in parentheses or brackets (ex '[t1]'). ๐
The op-flags (ex 'sK/2') are described in ("OP flags abbreviations"). ๐
% 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
The default rendering is top-down, so they're not in execution order. This form reflects the way the stack is used to parse and evaluate expressions; the add operates on the two terms below it in the tree. tree brain, not execution brain. ๐ธ
Nullops appear as "ex-opname", where opname is an op that has been optimized away by perl. They're displayed with a sequence-number of '-', because they are not executed (they don't appear in previous example), they're printed here because they reflect the parse. ๐ญ
The arrow points to the sequence number of the next op; they're not displayed in -exec mode, for obvious reasons. ๐
Note that because this rendering was done on a non-threaded perl, the PADOPs in the previous examples are now SVOPs, and some (but not all) of the square brackets have been replaced by round ones. This is a subtle feature to provide some visual distinction between renderings on threaded and un-threaded perls. sneaky but iconic. โจ
๐ OPTIONS ๐
Arguments that don't start with a hyphen are taken to be the names of subroutines or formats to render; if no such functions are specified, the main body of the program (outside any subroutines, and not including use'd or require'd files) is rendered. Passing BEGIN, UNITCHECK, CHECK, INIT, or END will cause all of the corresponding special blocks to be printed. Arguments must follow options. order matters, bestie. ๐
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 this document.
-basicโ Print OPs in the order they appear in the OP tree (a preorder traversal, starting at the root). The indentation of each OP shows its level in the tree, and the'->'at the end of the line indicates the next opcode in execution order. This mode is the default, so the flag is included simply for completeness.-execโ Print OPs in the order they would normally execute (for the majority 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 indentation. In cases like loops when control jumps out of a linear path, a'goto'line is generated.-treeโ Print OPs in 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 the line-style (or just style) used to render each opcode, and dictates what info is actually printed into each line.
-conciseโ Use the author's favorite set of formatting conventions. This is the default, of course.-terseโ Use formatting conventions that emulate the output ofB::Terse. The basic mode is almost indistinguishable from the realB::Terse, and the exec mode looks very similar, but is in a more logical order and lacks curly brackets.B::Tersedoesn't have a tree mode, so the tree mode is only vaguely reminiscent ofB::Terse.-linenoiseโ Use formatting conventions in which the name of each OP, rather than being written out in full, is represented by a one- or two-character abbreviation. This is mainly a joke.-debugโ Use formatting conventions reminiscent of CPAN moduleB::Debug; these aren't very concise at all.-envโ Use formatting conventions read from the environment variablesB_CONCISE_FORMAT,B_CONCISE_GOTO_FORMAT, andB_CONCISE_TREE_FORMAT.
Options for tree-specific formatting
-compactโ Use a tree format in which the minimum amount of space is used for the lines connecting nodes (one character in 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 better than the compact one, especially in ASCII, and is the default.-vtโ Use tree connecting characters drawn from the VT100 line-drawing set. This looks better if your terminal supports it.-asciiโ Draw the tree with standard ASCII characters like"+"and"|". These don't look as clean as the VT100 characters, but they'll work with almost any terminal (or the horizontal scrolling mode ofless(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. pick your fighter. ๐
Options controlling sequence numbering
-basenโ Print OP sequence numbers in 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 sequence numbers with the least significant digit first. This is obviously mutually exclusive with bigendian.
Other options
-srcโ With this option, the rendering of each statement (starting with thenextstateOP) will be preceded by the 1st line of source code that generates it. For example:
1 <0> enter
# 1: my $i;
2 <;> nextstate(main 1 junk.pl:1) v:{
3 <0> padsv[$i:1,10] vM/LVINTRO
# 3: for $i (0..9) {
4 <;> nextstate(main 3 junk.pl:3) v:{
5 <0> pushmark s
6 <$> const[IV 0] s
7 <$> const[IV 9] s
8 <{> enteriter(next->j last->m redo->9)[$i:1,10] lKS
k <0> iter s
l <|> and(other->9) vK/1
# 4: print "line ";
9 <;> nextstate(main 2 junk.pl:4) v
a <0> pushmark s
b <$> const[PV "line "] s
c <@> print vK
# 5: print "$i\n";
...
-stash="somepackage"โ With this,"somepackage"will be required, then the stash is inspected, and each function is rendered.
The following options are pairwise exclusive. ๐
-mainโ Include the main program in the output, even if subroutines were also specified. This rendering is normally suppressed when a subroutine name or reference is given.-nomainโ This restores the default behavior after you've changed it with'-main'(it's not normally needed). If no subroutine name/ref is given, main is rendered, regardless of this flag.-nobannerโ Renderings usually include a banner line identifying the function name or stringified subref. This suppresses the printing of the banner.
TBC: Remove the stringified coderef; while it provides a 'cookie' for each function rendered, the cookies used should be 1,2,3.. not a random hex-address. It also complicates string comparison of two different trees.
-bannerโ restores default banner behavior.-banneris => subrefโ TBC: a hookpoint (and an option to set it) for a user-supplied function to produce a banner appropriate for users needs. It's not ideal, because the rendering-state variables, which are a natural candidate for use inconcise.t, are unavailable to the user.
๐ Option Stickiness ๐
If you invoke Concise more than once in a program, you should know that the options are 'sticky'. This means that the options you provide in the first call will be remembered for the 2nd call, unless you re-specify or change them. so yeah, they come along for the ride unless you say otherwise. ๐
โจ ABBREVIATIONS โจ
Okay bestie, the concise style uses symbols to pack max info with minimal clutter (like hex addresses). With just a little practice, you can start to see the flowers, not just the branches, in the trees ๐.
๐ OP class abbreviations ๐
These symbols sit right before the op-name and tell you which B:: namespace represents the ops in your Perl code.
0โOP(akaBASEOP): an OP with no children.1โUNOP: an OP with one child.+โUNOP_AUX: a UNOP with auxillary fields.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.{โ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..โMETHOP: an OP with method call info.
๐ฉ OP flags abbreviations ๐ฉ
OP flags are either public or private. The public flags alter the behavior of each opcode in consistent ways, and they're represented by 0 or more single characters.
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).- (no symbol) โ Want is unknown.
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 (seeop.h).
Private flags, if any are set for an opcode, are displayed after a /:
8 <@> leave[1 ref] vKP/REFC ->(end)
7 <2> sassign vKS/2 ->8
They're opcode specific, and occur less often than the public ones, so they're represented by short mnemonics instead of single-chars; see B::Op_private and regen/op_private for more details.
Note that a number after a/often indicates the number of arguments. In thesassignexample above, the OP takes 2 arguments. These values are sometimes used at runtime: in particular, theMAXARGmacro makes use of 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 both basic and exec modes to print all opcodes. The 2nd, goto-format, is used in exec mode when branches are encountered. They're not real opcodes, and are inserted to look like a closing curly brace. The tree-format is tree specific.
When a line is rendered, the correct format-spec is copied and scanned for the following items; data is substituted in, and other manipulations like basic indenting are done, for each opcode rendered.
There are 3 kinds of items that may be populated: special patterns, #vars, and literal text, which is copied verbatim. (Yes, it's a set of s///g steps.)
๐ง Special Patterns ๐ง
These items are the primitives used to perform indenting, and to select text from amongst alternatives.
(x(exec_text;basic_text)x)โ generatesexec_textin exec mode, orbasic_textin basic mode.(*(text)*)โ generates one copy oftextfor each indentation level.(*(text1;text2)*)โ generates one fewer copies oftext1than the indentation level, followed by one copy oftext2if the indentation level is more than 0.(?(text1#varText2)?)โ if the value ofvaris true (not empty or zero), generates the value ofvarsurrounded bytext1andText2, otherwise nothing.~โ any number of tildes and surrounding whitespace will be collapsed to a single space.
๐ฎ # Variables ๐ฎ
These #vars represent opcode properties that you may want as part of your rendering. The '#' is intended as a private sigil; a #var's value is interpolated into the style-line, much like "read $this".
These vars take 3 forms:
#varโ a property named'var'is assumed to exist for the opcodes, and is interpolated into the rendering.#varNโ generates the value ofvar, left justified to fillNspaces. Note that this means while you can have properties'foo'and'foo2', you cannot render'foo2', but you could with'foo2a'. You would be wise not to rely on this behavior going forward ;-)#Varโ this ucfirst form of#vargenerates a tag-value form of itself for display; it converts'#Var'into a'Var => #var'style, which is then handled as described above. (Imp-note:#Varscannot be used for conditional-fills, because the=> #vartransform is done after the check for#Var's value).
The following variables are 'defined' by B::Concise; when they are used in a style, their respective values are plugged into the rendering of each opcode.
Only some of these are used by the standard styles, the others are provided for you to delve into optree mechanics, should you wish to add a new style (see add_style below) that uses them. You can also add new ones using add_callback.
#addrโ the address of the OP, in hexadecimal.#argโ the OP-specific information of the OP (such as the SV for an SVOP, the non-local exit pointers for a LOOP, etc.) enclosed in parentheses.#classโ the B-determined class of the OP, in 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 any.#exnameโ the name of the OP, or'ex-foo'if 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 hexadecimal.#flagsโ the OP's flags, abbreviated as a series of symbols.#flagvalโ the numeric value of the OP's flags.#hintsโ the COP's hint flags, rendered with abbreviated names if possible. An empty string if this is not a COP. Here are the symbols used:
$ strict refs
& strict subs
* strict vars
x$ explicit use/no strict refs
x& explicit use/no strict subs
x* explicit use/no strict vars
i integers
l locale
b bytes
{ block scope
% localise %^H
< open in
> open out
I overload int
F overload float
B overload binary
S overload string
R overload re
T taint
E eval
X filetest access
U utf-8
us use feature 'unicode_strings'
fea=NNN feature bundle number
#hintsvalโ the numeric value of the COP's hint flags, or an empty string if this is not a COP.#hyphseqโ the sequence number of the OP, or a hyphen if it doesn't have one.#labelโ'NEXT','LAST', or'REDO'if the OP is a target of one of those in exec mode, or empty otherwise.#lastaddrโ the address of the OP's last child, in hexadecimal.#nameโ the OP's name.#NAMEโ the OP's name, in all caps.#nextโ the sequence number of the OP's next OP.#nextaddrโ the address of the OP's next OP, in hexadecimal.#noiseโ a one- or two-character abbreviation for the OP's name.#privateโ the OP's private flags, rendered with abbreviated names if possible.#privvalโ the numeric value of the OP's private flags.#seqโ the sequence number of the OP. Note that this is a sequence number generated byB::Concise.#optโ whether or not the op has been optimized by the peephole optimizer.#sibaddrโ the address of the OP's next youngest sibling, in hexadecimal.#svaddrโ the address of the OP's SV, if it has an SV, in hexadecimal.#svclassโ the class of the OP's SV, if it has one, in all caps (e.g.,'IV').#svvalโ the value of the OP's SV, if it has one, in 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 any, otherwise the lettertfollowed by the OP's targ in decimal.#targarglifeโ same as#targarg, but followed by the COP sequence numbers that delimit the variable's lifetime (or'end'for a variable in an open scope) for a variable.#typenumโ the numeric value of the OP's type, in decimal.
๐ One-Liner Command tips ๐
perl -MO=Concise,bar foo.plโ renders onlybar()fromfoo.pl. To seemain, drop the',bar'. To see both, add',-main'.perl -MDigest::MD5=md5 -MO=Concise,md5 -e1โ identifiesmd5as an XS function. The export is needed so that BC can find it inmain.perl -MPOSIX -MO=Concise,_POSIX_ARG_MAX -e1โ identifies_POSIX_ARG_MAXas a constant sub, optimized to an IV. Although POSIX isn't entirely consistent across platforms, this is likely to be present in virtually all of them.perl -MPOSIX -MO=Concise,a -e 'print _POSIX_SAVED_IDS'โ this renders a print statement, which includes a call to the function. It's identical to rendering a file with ausecall and that single statement, except for the filename which appears in thenextstateops.perl -MPOSIX -MO=Concise,a -e 'sub a{_POSIX_SAVED_IDS}'โ very similar to previous, only the first two ops differ. This subroutine rendering is more representative, insofar as a single main program will have many subs.perl -MB::Concise -e 'B::Concise::compile("-exec","-src", \%B::Concise::)->()'โ this renders all functions in theB::Concisepackage with the source lines. It eschews the O framework so that the stashref can be passed directly toB::Concise::compile(). See-stashoption for a more convenient way to render a package.
๐ Using B::Concise outside of the O framework ๐
okay so the classic (and honestly original) usage of B::Concise was for command-line renderings of simple code, as given in EXAMPLE. But bestie, you can also use B::Concise straight from your own code, call compile() directly, and call it repeatedly. Doing that means you dodge the compile-time-only operation of O.pm, and you can even use the debugger to step through B::Concise::compile() itself. iconic.
Once you're in that mode, you can alter Concise output by adding new rendering styles, and optionally adding callback routines that populate new variables โ but only if those variables are referenced from those (just added) styles. iykyk.
๐ Example: Altering Concise Renderings ๐
use B::Concise qw(set_style add_callback);
add_style($yourStyleName => $defaultfmt, $gotofmt, $treefmt);
add_callback
( sub {
my ($h, $op, $format, $level, $stylename) = @_;
$h->{variable} = some_func($op);
});
$walker = B::Concise::compile(@options,@subnames,@subrefs);
$walker->();
โจ set_style() โจ
set_style takes 3 arguments, and updates the three format-specs that make up a line-style (basic-exec, goto, tree). It's got one minor drawback though: it doesn't register the style under a new name. That can become an issue if you render more than once and switch styles. So you may prefer add_style() and/or set_style_standard() instead. periodt.
๐ฆ set_style_standard($name) ๐ฆ
This one restores one of the standard line-styles into effect: "terse", "concise", "linenoise", "debug", "env". It also accepts style names previously defined with add_style(). slay.
๐ add_style () ๐
This subroutine accepts a new style name and three style arguments as above, and creates, registers, and selects the newly named style. It is an error to re-add a style; call set_style_standard() to switch between several styles. no cap.
๐ add_callback () ๐
If your newly minted styles refer to any new #variables, you'll need to define a callback subroutine that will populate (or modify) those variables. They are then available for use in the style you've chosen.
The callbacks are called for each opcode visited by Concise, in the same order as they are added. Each subroutine is passed five parameters:
- A hashref, containing the variable names and values which are populated into the report-line for the op
- the op, as a
B::OPobject - a reference to the format string
- the formatting (indent) level
- the selected stylename
To define your own variables, simply add them to the hash, or change existing values if you need to. The level and format are passed in as references to scalars, but it is unlikely that they will need to be changed or even used. fr fr.
๐ Running B::Concise::compile() ๐
compile accepts options as described above in "OPTIONS", and arguments, which are either coderefs, or subroutine names.
It constructs and returns a $treewalker coderef, which when invoked, traverses, or walks, and renders the optrees of the given arguments to STDOUT. You can reuse this, and can change the rendering style used each time; thereafter the coderef renders in the new style.
walk_output lets you change the print destination from STDOUT to another open filehandle, or into a string passed as a ref (unless you've built perl with -Uuseperlio).
my $walker = B::Concise::compile('-terse','aFuncName', \&aSubRef); # 1
walk_output(\my $buf);
$walker->(); # 1 renders -terse
set_style_standard('concise'); # 2
$walker->(); # 2 renders -concise
$walker->(@new); # 3 renders whatever
print "3 different renderings: terse, concise, and @new: $buf\n";
When $walker is called, it traverses the subroutines supplied when it was created, and renders them using the current style. You can change the style afterwards in several different ways:
- call
compile, altering style or mode/order - call
set_style_standard - call
$walker, passing@newoptions
Passing new options to the $walker is the easiest way to change amongst any pre-defined styles (the ones you add are automatically recognized as options), and is the only way to alter rendering order without calling compile again. Note however that rendering state is still shared amongst multiple $walker objects, so they must still be used in a coordinated manner. lowkey important.
๐ B::Concise::reset_sequence() ๐
This function (not exported) lets you reset the sequence numbers (note that they're numbered arbitrarily, their goal being to be human readable). 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, B::Concise, seeing that they're separate optrees, generates different sequence numbers in the output. ๐ซถ
๐ Errors ๐
Errors in rendering (non-existent function-name, non-existent coderef) are written to the STDOUT, or wherever you've set it via walk_output().
Errors using the various style calls, and bad args to walk_output(), result in die(). Use an eval if you wish to catch these errors and continue processing. not me having to catch errors with eval ๐
๐ธ AUTHOR ๐ธ
Stephen McCamant, <smcc@CSUA.Berkeley.EDU>.
And that's the whole vibe, bestie โ this page is from perl v5.42.3, dated 2026-11-03, and it's the B::Concise(3pm) manpage. ๐โจ
๐ฆ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms