The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Name

B::DeparseTree

Synopsis

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

    # create a coderef "none"
    sub none() { return };

    # Get full parse tree after deparsing
    $deparse->coderef2info(\&none);

    # returns:
    # \ {
    #    body        [
    #    [0] {
    #        body    [
    #            [0] {
    #                cop      B::COP=SCALAR(0x1c8cc78)  {
    #                    Parents       B::OP
    #                     ...
    #                },
    #                op       LISTOP=SCALAR(0x20fe600)
    #                parent   12857296,
    #                text     "",
    #                texts    [],
    #                type     "nextstate"
    #            },
    #            [1] {
    #                cop      B::COP=SCALAR(0x1c8cc78)
    #                op       B::LISTOP  {
    #                    ...
    #                    Parents       B::BINOP
    #                ...
    #            }
    #        ...
    #   sep         "",
    #        text        "() {
    #            return;
    #       }",
    #   texts       [
    #      [0] "() ",
    #      [1] "{
    #      ",
    #      [2] "
    #        return;",
    #      [3] "
    #      }"
    #    ],
    #   type "sub"
    # ...


    # Now show the text for what was deparsed. Note
    # The subroutine name is missing so we fill that in:
    print("none", $dp->{text}, "\n");

    none() {
      return;
    }

    # We can print text for some part of the tree such as that LISTOP:

    print $dp->{body}[0]->{body}[1]->{text}, "\n";  # prints "return";

    # We can print results of op address you know about or may have found out about
    # from a prior deparse:

    my $dp2 = $deparse->coderef2info(\&none, 0x20fe600); # see above for that hex addr

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 get exactly 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: