Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re2: line number

by jkahn (Friar)
on Sep 12, 2002 at 22:55 UTC ( [id://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?

    -Blake

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.

      -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://197384]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-18 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found