Re^4: Line numbers
by chromatic (Archbishop) on Mar 17, 2012 at 00:02 UTC
|
It is non-obvious why (caller(0))[2] should work but caller(0)[2] should fail.
Precedence.
| [reply] [d/l] [select] |
|
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'
| [reply] [d/l] [select] |
|
I understand that light may act as a wave or particle depending on how you study it.
Certainly, but unlike particle physics, Perl comes with a manual.
I'm not claiming that slicing the list returned from a function has syntax that's immediately obvious to everyone, but it's not that obscure once you understand Perl's precedence.
| [reply] |
|
So you exchange parens for square brackets and add an arrow instead. Sorry, but you really haven't proven your point. They are both non-obvious until you understand how Perl handles precedence and lists. caller(0)[0] fails because you are trying to index a scalar, not a list.
| [reply] |
|
|
Well he rather meant "intuitive", all examples are about precedence.
| [reply] |
|
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];
| [reply] [d/l] [select] |
|
> is expanding to:
> say $package, $filename, $line, $subroutine, $hasargs,$wantarray, $evaltext, $is +_require, $hints, $bitmask, $hinthash,[0];
nope, and you're showing valid code! After the last comma comes a literal arrayref [0].
DB<100> print $a,[0];
ARRAY(0x84e16b8)
the syntax-error comes from appending [0] unseparated to a function call say().
and nothing is "expanded" at parsing time.
| [reply] [d/l] [select] |
|
|
|
|
Actually, this is wrong!
When you say:
print (caller(0))[0],
print is interpretated as a function (equaivalent to print('hi')[0]), but it works fine if you say:
print '', (caller(0))[0]
| [reply] [d/l] [select] |
Re^4: Line numbers
by LanX (Saint) on Mar 17, 2012 at 00:08 UTC
|
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.
| [reply] |
|
Anyway I'd rather prefer a clean named argument solution instead of those silly positional parameters of caller.
Devel::StackTrace
| [reply] |