The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

B::DeparseTree

Synopsis

   use B::DeparseTree;
   my $deparse = B::DeparseTree->new();

   # create a subroutine to deparse...
   sub my_abs($) {
       return -$a < 0 ? -$a : $a;
   };

   my $deparse_tree = B::DeparseTree->new();
   my $tree_node = $deparse_tree->coderef2info(\&my_abs);
   print $tree_node->{text};

The above produces:

    ($)
    {
        return -$a < 0 ? -$a : $a
    }

but the result are reconstructed purely from the OPnode tree. To show parent-child information in the tree:

   use B::DeparseTree;
   B::DeparseTree::Fragment::dump_relations($deparse_tree);

which produces in abbreviated form...

   ...
   3--------------------------------------------------
   Child info:
        addr: 0x263b710, parent: 0x263b748
        op: pushmark
        text: return -$a < 0 ? -$a : $a

   return -$a < 0 ? -$a : $a
   ||||||
   3--------------------------------------------------
   4--------------------------------------------------
   Child info:
        addr: 0x263b748, parent: 0x263b628
        op: return
        text: return -$a < 0 ? -$a : $a

   return -$a < 0 ? -$a : $a
   -------------------------
   4--------------------------------------------------
   5--------------------------------------------------
   Child info:
        addr: 0x263b790, parent: 0x263b748
        op: cond_expr
        text: -$a < 0 ? -$a : $a

   return -$a < 0 ? -$a : $a
          ------------------
   5--------------------------------------------------
   ...
   9--------------------------------------------------
   Child info:
        addr: 0x263b898, parent: 0x263b7d0
        op: negate
        text: -$a

   return -$a < 0 ? -$a : $a
                    ---
   9--------------------------------------------------

The subroutine extract_node_info is what produces the text with its underline.

Description

Perl's B::Deparse but we save abstract tree information and associate that with Perl text fragments. These are fragments accessible by OP address. With this, in Perl you can determine more precisely where you in a program with granularity finer that at a line number boundary.

Uses for this could be in stack trace routines like Carp. It is used in the deparse command extension to Devel::Trepan.

See also: