in reply to Re: line number
in thread line number

How is that different from:
$linenum = __LINE__;

-Blake

Replies are listed 'Best First'.
Re: Re2: line number
by jkahn (Friar) on Sep 12, 2002 at 22:55 UTC
    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.
        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