I've read and understood that it's not possible to parse Perl 5 code without a Perl interpreter because of the BEGIN
blocks.
I know there are standalone parsers like
PPI
(used by Perl::Critic
)
that approximately parses the initial document.
But I couldn't find any reference to the procedure for outputting some kind of abstract syntax tree, or whatever internal structure resembling a tree that Perl uses for the execution phase.
It would be interesting to compare the internal representation for the parse and execution phases to see the code expand and change.
So basically there are a few options:
For static analysis, use PPI, which has the downside of not processing Perl code at BEGIN
blocks. This would be analogous to "parsing".
For dynamic analysis, use Use B::Concise to get the internal opcodes used for execution. This would be analogous to "compiling" (to the Perl VM).
A combination of both would be using B::Deparse to get Perl code of the internal representation (instead of VM opcodes), and then send it to PPI for building the tree.
EDIT: clarified point 3 based on comment. Still don't know a command that would expand imports and output a single big file (so that syntax extensions are resolved).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With