Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Re2: line number

by jkahn (Friar)
on Sep 12, 2002 at 22:55 UTC ( #197384=note: print w/replies, xml ) Need Help??

in reply to Re2: line number
in thread line number

dmitri said:
$linenum = &{sub{(caller)[2]}};
blakem said:
$linenum = __LINE__;

dmitri's solution returns the line number of the call tothe line in the current subroutine.

blakem's solution returns the line number of the __LINE__ within the current subroutine.

Amended 4:10p PDT I'm not sure why dmitri is building a subroutine reference only to call it. dmitri, do you care to clarify?

At 4:15, after reading blakem's follow-up, I realize blakem is right. dmitri's would be different, if he'd said:

$linenum = (caller)[2];
but he didn't. So they are equivalent, and I must say that __LINE__ is much simpler.

Replies are listed 'Best First'.
Re:4: line number
by blakem (Monsignor) on Sep 12, 2002 at 23:09 UTC
    dmitri's solution returns the line number of the call to the current subroutine.
    Yes, but the two are the same. The "current subroutine" (as far as caller() is concerned) is the one created right there on that line.
    $linenum = &{sub{(caller)[2]}};
    Its creating and calling a sub whose sole purpose is to find the linenumber of its caller.

    Its like phoning a friend to ask what number shows up on his caller ID... Why bother when your own phone number is pasted right there on the front of your phone?


Re: Re: Re2: line number
by dmitri (Priest) on Sep 13, 2002 at 16:17 UTC
    I dont have the code where I used this thing. I remember that for some reason I could not use __LINE__; it could have been within an eval.
      OK, thats what I was interested in. Both __LINE__ and caller() border on the black magic side of perl (grey magic anyone???) I was wondering if you had found a place where the two didn't agree.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://197384]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-03-23 13:15 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (293 votes). Check out past polls.