Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Line numbers

by LanX (Saint)
on Mar 16, 2012 at 23:18 UTC ( [id://960091]=note: print w/replies, xml ) Need Help??


in reply to Re: Line numbers
in thread Line numbers

is there any advantage of [caller(0)]->[2] over (caller(0))[2]?

Cheers Rolf

Replies are listed 'Best First'.
Re^3: Line numbers
by tobyink (Canon) on Mar 16, 2012 at 23:33 UTC

    Indeed there is an advantage of the first - I can remember the syntax.

    It is non-obvious why (caller(0))[2] should work but caller(0)[2] should fail.

    Also, the arrayref plays nicer associativity wise. Check this out:

    $ perl -E'sub x { say [caller(0)]->[0] } x()' main $ perl -E'sub x { say (caller(0))[0] } x()' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.

    The version with the arrayref just acts more predictably in my mind. I don't have to waste time thinking about it.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      It is non-obvious why (caller(0))[2] should work but caller(0)[2] should fail.

      Precedence.

        That there exists an explanation does not imply that the explanation is obvious.

        I understand that light may act as a wave or particle depending on how you study it. However, this fact is not obvious. It is in fact incredibly non-obvious, which is how it took science so long to get there.

        That [caller(0)]->[0] works is (at least to me) obvious. That (caller(0))[0] works and caller(0)[0] fails is (again, to me) non-obvious.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Well he rather meant "intuitive", all examples are about precedence.

        Cheers Rolf

        Right. The added complexity actually comes from introducing the say function, which takes a list.

        say (caller(0))[0];

        is expanding to:

        say $package, $filename, $line, $subroutine, $hasargs,$wantarray, $eva +ltext, $is_require, $hints, $bitmask, $hinthash,[0];

        This gives you the syntax error:

        say [0];
      Thanks!

      Personally I don't like referencing for immediate dereferencing.

      But that's a matter of taste...

      Anyway I'd rather prefer a clean named argument solution instead of those silly positional parameters of caller.

      Cheers Rolf

          Anyway I'd rather prefer a clean named argument solution instead of those silly positional parameters of caller.

        Devel::StackTrace

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found