Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

B::Deparse and sub line numbers

by Anonymous Monk
on Oct 29, 2012 at 13:55 UTC ( #1001361=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use B::Deparse's output to find out where a subroutine is defined. It seems the line numbers indicate where the subroutine code ends but not where it starts. Here is my test program:
use strict; use warnings; #3 sub foo () { print("F1"); print("F2"); print("F3"); } #9
And here is "perl -MO=Deparse,-l" output:
#line 8 "" sub foo () { use warnings; use strict 'refs'; #line 5 "" print 'F1'; #line 6 "" print 'F2'; #line 7 "" print 'F3'; } syntax OK
So the "#line" before the sub declaration is in fact the end of the sub, not its begin. How can I find out where it really starts knowing that pragmas like "strict" do change how the sub is rendered?

Comment on B::Deparse and sub line numbers
Select or Download Code
Re: B::Deparse and sub line numbers
by toolic (Chancellor) on Oct 29, 2012 at 14:21 UTC
    I do not know how to convince B::Deparse (or B::Xref for that matter) to give you the starting line number. What do you plan to do with that information once you have it? Maybe the ideas in Re: 'extract' subs from source code can help. Perhaps if you poke around PPI you can figure out how to get the line numbers.

      The goal is to use B::Deparse to check some properties of the code (kind of lint). The line numbers are needed to report precise information to the developer.

      FWIW, yes, I'm aware that B::Lint and Perl::Critic exist.

      I will look at PPI but this seems to be quite large...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001361]
Approved by marto
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (17)
As of 2014-07-14 12:38 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (259 votes), past polls