The elements of @mass_info are references to annonymous hashes. If you were to just print out the value of $_, you would see something like this:
HASH(0x804e054)
The first snippet of code fails because you are trying to access the 'PID' element of the %_ hash, as this does not exist, then you get nothing output. If you had run perl with warnings on (-w) then you would have seen a warning about the use of an unitialised value where this was used.
The second example works because you are dereferencing the reference held in $mass_info[0] by the use of the {PID}. Another way to write this is as $mass_info[0]->{PID} which is clearer, as it shows that you are accessing the PID element of the hash which is 'pointed' too by $mass_info[0].
Your first loop can be made to work by using this syntax:
foreach (@mass_info) {
print $_->{PID}, "\n";
}
If you had a hash with many elements stored in $mass_info[0] then you would need to iterate over the values held in it using a syntax like this:
foreach my $hash_ref (@mass_info) {
foreach (keys %{$hash_ref}) {
print "$_ => ${$hash_ref}{$_}\n";
}
}
For more information, you should refer to the perldsc, perllol, perlreftut and perlref manpages. You have uncovered one of the most powerful features of perl, the way to easily create complicated data structures. |