my @test = ( 0 );
if ( defined @{ $test[1] }[1] ){}
my $l = @test;
print $l;
produced a "2".
my @test = ( 0 );
if ( defined $test[1] && defined @{ $test[1] }[1] ){}
my $l = @test;
print $l;
and all is fine again, but where can I find any documentation of this behaviour? It was unexpected to me and I would like to read more about it. Neither perlref nor the perlreftut seemed to document that an element is appended to @test as soon as it is accessed.Perhaps your code would look better with [exists] rather than [defined].
#!/usr/bin/perl -w
use strict;
my @test = ( 0 );
my $len = 0;
if ( exists $test[1]->[1] ) {
$len = @test;
}
print $len;
if (ref $test[1] and exists $test[1][1]) {}
It occurs to me that a sub that returns the passed value iff it is a ref might provide some shorthand for this idiom:
sub iff_ref {
return ref $_[0] ? $_[0] : undef;
}
# Then your test becomes (if exists doesn't mind the notation)
if (exists iff_ref($test[1])->[1]) {}
# or it might have to be
if (exists ${iff_ref($test[1])}[1]) {}
I don't have perl5 where I am today, so this is absolutely untested.
perlmonks.org content © perlmonks.org and pelagic, PerlingTheUK, rnahi, Roy Johnson
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03