Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the documentation for the behavior of @array->[4] or %hash->{key} in Perl?

Tags:

syntax

perl

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
like image 580
Eric Strom Avatar asked Feb 26 '23 11:02

Eric Strom


1 Answers

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
like image 59
Eric Strom Avatar answered May 08 '23 23:05

Eric Strom