Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Well, the first question to ask is, "can I run the code in order to find out more about it". This isn't just a question of security, mind you -- some scripts take action at BEGIN time that you'd prefer to avoid.

If you can't, you run afowl of the meaning of the "Only perl can parse Perl" quote -- as something other then the perl interpreter (even if it's written in Perl) trying to parse Perl (the language), you aren't going to be able to do a perfect job. You can do a /decent/ job, using complex regexes, PPI, or even simple regexes and Text::Balanced.

If you /can/, then a whole world of introspection opens up to you. perl has already parsed the program (or will parse the program, depending on when you're talking about, which depends on how you structure your program.) You can walk the symbol tables to find your subs, and use some B magic to find the begin and end lines.

To whit: Given a coderef, running B::svref_2object on it will give you an object somewhere in the B tree (I don't quite recall; B::SVRV quite likely; let's call it $cv). Running $cv->GV->LINE, according to my comments, gives you the line number of the /last/ line of the sub. $cv->FILE; will give you the filename. $cv->START; will give you the opcode where execution starts. If this ISA B::COP (and it always will be), then you can call ->line on it, to get it's line number. IIRC, that's the line number of the first /statement/ within the sub, not of the sub foo { line.

Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

In reply to Re: Extract subroutines from a Perl script. OR: "Only perl can parse Perl." But can it help me to do so? by theorbtwo
in thread Extract subroutines from a Perl script. OR: "Only perl can parse Perl." But can it help me to do so? by muba

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2023-06-08 04:56 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (29 votes). Check out past polls.