views:

93

answers:

1

A recent question used a sigil invariant syntax %hash->{key} = 1; for hash access, which seems to work fine, but I would have thought it would be a syntax error.

It seems to work for arrays as well:

my @array;

@array->[3] = 6;

Is this behavior documented somewhere? I don't remember reading it, but may have overlooked it.

It seems to behave exactly like:

(\%hash)->{key}

rather than what I would have assumed:

(scalar %hash)->{key}  # runtime error
+8  A: 

Seems this was covered over at perlmonks: http://www.perlmonks.org/?node_id=171177

My reading of perlop has me convinced that this is an unintended
syntactic feature.

And that's exactly what it is. When using the arrow, Perl will see
whatever is left of it as a reference. Including if you have something
like @l or %h.

Note that you will get the warning
Using an array as a reference is deprecated in Perl 5.8.0.

  Abigail
Eric Strom