Hello monks,

With B::Concise, it's possible to get a syntax tree for specific subroutines, CODE refs, or the top "main" program. However, what I need is the syntax tree (or trees) for an entire module. I.e., I need some kind of dump of Perl's parse of a module, which might include "dead" code that is never called. I would also want to recursively do the same thing for any other modules/sources pulled in with use or require (I'd probably ignore core modules and a few others, but haven't decided yet).

This will be part of a sort of static analysis tool I'm working on. This therefore imposes the constraint that the target module is essentially unknown code, and should be considered 'read-only' in the sense that I can't require the developer to add instrumentation to the code itself.

Also, requiring a recompile of perl is not an option since this code will be widely distributed. So, unfortunately all perl -D flags are a no-go.

One rather ugly hack I explored briefly was to try to extract all sub names and use/require modules from the source and run B::Concise on each result, but, beyond the fact it's a terrible idea, there's no way that I know of to run B::Concise on anon subs without their compiled CODE ref handy, which I wouldn't, and couldn't, in general, compile. (Whenever sub { ... } shows up in the code, the entire sub just shows up as a single anoncode line.)

Is there a way to do what I'm describing? Efficiency is not high on my list of priorities.


In reply to Optree for entire module by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.