Can one blame the user if he can do
push @{$h{$_}}, 44;
but has a hard time guessing that only the final one of
$#{$h{$_}}, $#{@{$h{$_}}}, $#@{$h{$_}}
will tell him how many elements are in the array where he pushed it?
Yes. None of those will tell him how many elements are in the array.
Only the first is valid Perl, and gives the "highest index", which is
normally one less than the number of elements in the array.
The second gives me "Can't use string ("1") as an ARRAY ref while
"strict refs" in use at jidanni.pl line 12.", because an array
evaluated in scalar context returns the number of elements in the
array, and my test array had one element. (However, I just tried "perl
-Mstrict -e 'print $#{'1'};'", and that doesn't give me an error, so
maybe there is some other reason for the error.)
The third won't even compile:
"$# is no longer supported at jidanni.pl line 13.
Array found where operator expected at jidanni.pl line 13, at end of
line
(Missing operator before ?)
Can't use an undefined value as a symbol reference at jidanni.pl line
13."
Test program:
#!/usr/local/bin/perl
use strict;
use warnings;
my %h;
$_ = 'A';
$h{$_} = [];
push( @{$h{$_}}, 44 );
print $#{$h{$_}};
print $#{@{$h{$_}}}; # Can't use string ("1") as an ARRAY ref ...
print $#@{$h{$_}}; # no longer supported ...
There is no need to guess. The rule is that if @{X} is an array, then
$#{X} is the highest index of the array. 'X' can be a bare-word array
name or an array reference. If the former, then the braces {} are
optional. If X is a simple variable, e.g. $x, then the braces are also
optional.