I’m currently working in a very complex Perl architecture, and I want to create some debugging tools. Since a lot of the behavior involves anonymous subroutines, I’d like to analyze some of the behavior, and all I have to work with is a reference to the subroutine.
In short, is there a way to print the code (since Perl is interpreted it may still be available?) of a subroutine reference?
The core module B::Deparse provides this functionality.
which prints:
When using
B::Deparseit is important to remember that what it returns is a decompiled version of the compiled tree of op-codes, not the original source text. This means that constants, arithmetic expressions, and other constructs may be folded and rewritten by the optimizer.The other part of the puzzle is dealing with closed over lexical variables. If the subroutines you are working with access any external lexicals, they will not be present in the output of deparse, and will cause recompilation to fail. You can solve this with the
closed_overandset_closed_overfunctions from the PadWalker module.Finally, if you want to find out where the subroutine’s real source code is, you can use the core B module:
which prints something like: