Richard Harman <> wrote in
news

:
> I think I found a bug in perl when dealing with hash slices. For some
May people have thought they found bugs in perl. Few of them really
have.
> 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?)
Yes, it's the way it's supposed to work. The entry "What is the
difference between a list and an array?" in perlfaq4 is a good place to
start.
> # 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);
@array_refs is an array, and so when put in scalar context evaluates to
the number of elements. That's a property of *arrays* in particular.
>
> # 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)});
Your slice is indeed a list, but it's not an array, so it doesn't have
that particular (peculiar?) property.
> # but this works (forced list interpolation)
> printf("Scalar hashref forced list interpolation RIGHT: %s\n",
> scalar @{[@$hashref{qw(first second)}]});
You've actually created an array here.