B::Concise(3perl)

πŸ¦‹ NAME πŸ¦‹

B::Concise β€” walk the Perl syntax tree and print concise info about ops. That's it, that's the whole vibe. (This one's from the Perl Programmers Reference Guide, section 3perl, bestie πŸ’–)


✨ SYNOPSIS ✨

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

use B::Concise qw(set_style add_callback);

πŸ’œ DESCRIPTION πŸ’œ

Okay so picture this: this compiler backend prints the internal OPs of a Perl program's syntax tree, in one of several space-efficient text formats, and it's literally made for debugging the inner workings of perl or of other compiler backends. 🫢

It can print OPs in the order they appear in the OP tree, in the order they'll execute, or as a text approximation of their tree structure β€” and the format of the info displayed? Fully customizable. Its function is similar to perl's -Dx debugging flag or the B::Terse module, but it's more sophisticated and flexible. No cap. πŸ‘‘


🌸 EXAMPLE 🌸

Here's two outputs (or 'renderings'), using the -exec and -basic (i.e. default) formatting conventions on the same code snippet. Same code, two whole different lewks πŸ’…

% 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.

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.

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. 🧚


πŸŽ€ 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. πŸ“

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 of B::Terse. The basic mode is almost indistinguishable from the real B::Terse, and the exec mode looks very similar, but is in 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 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. 😭 (ngl iconic behaviour)
  • -debug β€” Use formatting conventions reminiscent of CPAN module B::Debug; these aren't very concise at all. πŸ’€
  • -env β€” Use formatting conventions read from the environment variables "B_CONCISE_FORMAT", "B_CONCISE_GOTO_FORMAT", and "B_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 of less(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 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 the nextstate OP) 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 in concise.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. πŸ™ˆ

fr fr, keep that in mind or your second rendering will be giving choices you didn't even make πŸ’…

✨ ABBREVIATIONS ✨

okay bestie so the concise style uses little symbols to give you max info with minimal clutter (like hex addresses). With just a little practice, you'll start to see the flowers, not just the branches, in the trees. 🌸

πŸŽ€ OP class abbreviations πŸŽ€

These symbols show up before the op-name, and they tell you which B:: namespace represents the ops in your Perl code. πŸ’…

  • 0 β€” OP (aka BASEOP): 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). 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 (see op.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: a number after a / often indicates the number of arguments. In the sassign example above, the OP takes 2 arguments. These values are sometimes used at runtime: in particular, the MAXARG macro 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) β€” Generates exec_text in exec mode, or basic_text in 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 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.
  • ~ β€” 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 of var, left justified to fill N spaces. 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 #var generates 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: #Vars cannot be used for conditional-fills, because the => #var transform 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 by B::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 letter t followed 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 only bar() from foo.pl. To see main, drop the ,bar. To see both, add ,-main.
  • perl -MDigest::MD5=md5 -MO=Concise,md5 -e1 β€” Identifies md5 as an XS function. The export is needed so that BC can find it in main.
  • perl -MPOSIX -MO=Concise,_POSIX_ARG_MAX -e1 β€” Identifies _POSIX_ARG_MAX as 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 -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 a use call and that single statement, except for the filename which appears in the nextstate ops.
  • perl -MPOSIX -MO=Concise,a -e 'sub a{print _POSIX_SAVED_IDS}' β€” This is 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 the B::Concise package with the source lines. It eschews the O framework so that the stashref can be passed directly to B::Concise::compile(). See -stash option for a more convenient way to render a package.

πŸ¦‹ Using B::Concise outside of the O framework πŸ¦‹

So the classic β€” like, the original β€” way everyone used B::Concise was for command-line renderings of simple code, exactly like the stuff in EXAMPLE above. Very demure, very mindless. πŸŽ€

But bestie, you can ALSO call B::Concise straight from your own code, and call compile() directly, and call it over and over. The way that this works is genuinely iconic: by doing it yourself, you skip the compile-time-only operation of O.pm entirely, and you can even fire up the debugger to step through B::Concise::compile() itself. πŸ’…

And once you're living in that world, you can mess with the Concise output by adding brand-new rendering styles, and optionally by adding callback routines that populate new variables β€” that is, if those new variables got referenced from the styles you just added. πŸ™ˆ


✨ 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 β€” that's basic-exec, goto, and tree. 🌸

It has one teeny-tiny drawback though: it doesn't register the style under a new name. Which lowkey becomes a whole problem if you render more than once and then switch styles around. So honestly? You might prefer to use add_style() and/or set_style_standard() instead. Periodt. πŸ’…


πŸ’œ set_style_standard($name)

This one restores one of the standard line-styles β€” "terse", "concise", "linenoise", "debug", "env" β€” right back into effect. ✨

It also accepts style names you previously defined with add_style(). So your custom lil styles get the same glow-up treatment. 🫢


🌈 add_style ()

This subroutine takes a new style name and three style arguments (same as above), then it creates, registers, and selects the newly named style. All in one go, main character energy. πŸ‘‘

Not me re-adding a style though β€” it is an error to re-add a style. If you wanna bounce between several styles, call set_style_standard(). 🚫

πŸŽ€ add_callback ()

If your freshly minted styles refer to any new #variables, you're gonna need to define a callback subroutine that populates (or modifies) those variables. Once you do, they're available for use in the style you've chosen. Slay. ✨

The callbacks get called for each opcode visited by Concise, in the same order as they are added. And each subroutine gets passed five parameters:

  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::OP object
  3. a reference to the format string
  4. the formatting (indent) level
  5. the selected stylename

To define your own variables, just 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 honestly it's unlikely they'll need to be changed or even used. πŸ’€


🌟 Running B::Concise::compile()

compile accepts options just like the ones described above in "OPTIONS", plus arguments β€” and those arguments are either coderefs, or subroutine names. πŸ“

It constructs and returns a $treewalker coderef, which, when you invoke it, traverses (or walks) and renders the optrees of the given arguments to STDOUT. And you can reuse this thing! You can change the rendering style used each time, and after that the coderef renders in the new style. 🌈

walk_output lets you change the print destination away from STDOUT to another open filehandle β€” or into a string passed as a ref. (Unless you've built perl with -Uuseperlio, that is. 🫠)

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:

  1. call compile, altering style or mode/order
  2. call set_style_standard
  3. call $walker, passing @new options

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 it's the only way to alter rendering order without calling compile again. πŸ”₯

Okay but real talk β€” rendering state is still shared amongst multiple $walker objects, so they must still be used in a coordinated manner. Don't be chaotic about it. πŸ™ˆ

🧚 B::Concise::reset_sequence()

This function (not exported, so don't @ me) lets you reset the sequence numbers. And yes, note that they're numbered arbitrarily β€” the whole goal is just 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 sees that they're separate optrees and generates different sequence numbers in the output. Which, for testing, is giving unhelpful. πŸ’€


πŸ’€ Errors

Errors in rendering (non-existent function-name, non-existent coderef) get 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(). So if you wanna catch these errors and keep processing, use an eval. Live your life, but wrap it. 🫢


πŸ‘‘ AUTHOR πŸ‘‘

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


okay that's the whole page bestie, we ate and left no crumbs πŸ’… this was perl v5.44.0, dated 2026-10-17, and it's the manual for B::Concise(3perl) β€” go forth and render optrees, you gorgeous genius πŸ¦‹βœ¨

πŸ“¦ where this page comes from
  • arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms