R
Richard Harman
I think I found a bug in perl when dealing with hash slices. For some
reason, scalar is interpreting the returned list from a hash slice as a
'scalar comma expression' and is returning the final element (perldoc -f
scalar, 3rd paragraph). Is this the way it's supposed to work? (If this
is not the correct place to ask, where do I go?)
# data
my %hash = ( first=>["Studly","Caps"], second=>["Make","Things","Readable"]);
my $hashref = \%hash;
# slicing the hashref for values (the arrays)
my @array_refs = @$hashref{qw(first second)};
# print out the memory addresses
printf "Contents of hash:\n";
foreach my $key (keys %hash) { print "$key => $hash{$key}\n"; };
printf("Scalar array_refs: %s\n",scalar @array_refs);
# now why doesn't this return 2? A slice is just a list, right?
printf("Scalar hashref slice WRONG: %s\n",scalar @$hashref{qw(first second)});
# but this works (forced list interpolation)
printf("Scalar hashref forced list interpolation RIGHT: %s\n",
scalar @{[@$hashref{qw(first second)}]});
reason, scalar is interpreting the returned list from a hash slice as a
'scalar comma expression' and is returning the final element (perldoc -f
scalar, 3rd paragraph). Is this the way it's supposed to work? (If this
is not the correct place to ask, where do I go?)
# data
my %hash = ( first=>["Studly","Caps"], second=>["Make","Things","Readable"]);
my $hashref = \%hash;
# slicing the hashref for values (the arrays)
my @array_refs = @$hashref{qw(first second)};
# print out the memory addresses
printf "Contents of hash:\n";
foreach my $key (keys %hash) { print "$key => $hash{$key}\n"; };
printf("Scalar array_refs: %s\n",scalar @array_refs);
# now why doesn't this return 2? A slice is just a list, right?
printf("Scalar hashref slice WRONG: %s\n",scalar @$hashref{qw(first second)});
# but this works (forced list interpolation)
printf("Scalar hashref forced list interpolation RIGHT: %s\n",
scalar @{[@$hashref{qw(first second)}]});