[Israel.pm] Correct way of taking a slice of a hash reference

Peter Gordon peter at pg-consultants.com
Mon Jan 21 00:59:52 PST 2008

This is a bit off topic - but anyway ....
Can someone explain, what appears to me to be bizarre behaviour.

Normally, Perl won't produce runtime fatal errors if the syntax is
correct. Why can't the two lines marked below just return undef. 

The failure on @$q is particularly irritating. If a reference to an
array is passed and the value happens to be undef because there were no
entries, you need to either check that the value is a reference to an
array, or to do an eval. Good programming practice would say the the
original value should be initialized correctly. But I would prefer Perl
to just me undef.

And if you are going to argue that this is correct behaviour, then why
does $s->{aaa} and co work?

use strict ;

my $s ; 
my @t = %$s ; # <-- This causes a fatal error
my $t1 = $s->{aaa} ; # This works

my $q ; 
my @w = @$q ; <-- This causes a fatal error
my $t2 = $q->[10] ; # This works


More information about the Perl mailing list