in reply to line number

Here's a quicky I sometimes use:

$linenum = &{sub{(caller)[2]}};

Replies are listed 'Best First'.
Re2: line number
by blakem (Monsignor) on Sep 12, 2002 at 22:50 UTC
    How is that different from:
    $linenum = __LINE__;

    -Blake

      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.
        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

        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.