in reply to Re^2: Hash syntax
in thread Hash syntax
> I have no problem using or understanding that noisier indirection syntax, but there must be an explanation beyond my current understanding of the arrow operator. Thoughts please?
Symmetry!
$arr_ref->[@slice] doesn't "work" because the analog $arr[@slice] also doesn't do what you mean.
NB: @arr[@slice] is different.
Perl is pretty consistent that $ means scalar and @ means a list. The whole context rules depend on it. Something like $ref->[@slice] is as misleading as $arr[@slice] because a scalar has to be returned. (What's happening here is that the scalar value of @slice is calculated instead of a list)
For a slice the correct syntax is @arr[@slice]
Compare the analogy
DB<12> @a = "a".."z"; $ar = \@a DB<13> p $ar->[1], $a[1] bb DB<14> @slice = 1..3 DB<15> p $ar->[@slice], $a[@slice] dd DB<16> p $ar->@[@slice], @a[@slice] bcdbcd DB<17> p @$ar[@slice], @a[@slice] bcdbcd
The deref syntax in line 16 is newer and was introduced to avoid @{...} constructs like in line 17 (In this case the brackets are optional)
What you are suggesting would break the symmetry and cause confusion and bugs.
Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery
Added short answer
|
|---|