in reply to A better understanding of array and hash references
One thing that hasn't been mentioned yet in this thread is that in your example, you are only looking at one value at a time: either the hash key 'a' or the array index 0. If this is true in your real code as well, then you don't need to be using slices. In fact I would say you should not use slices for a couple of reasons:
To see what I mean by that last one, try this code:
my $hashref = {}; # initializer not needed, but helps clarity $hashref->{a} = localtime; # can also be written $$hashref{a} print "$hashref->{a}\n"; @$hashref{a} = localtime; # this is a slice because of the @ print "$hashref->{a}\n";
The second assignment evaluates localtime in list context, but because the slice on the left side only has one element, only the first value in the returned list gets stored there, which happens to be the number of seconds.
My preference is to be as explicit as possible so I write $hashref->{a} for single elements and @{$hashref}{'a','b'} for slices.
|
|---|