DBX has asked for the wisdom of the Perl Monks concerning the following question:
I have always been under the impression that this if construct prevents perl from dying if $arrayref is undefined or not an actual array reference. I came across some code today, however, that makes me question that belief.if(defined @$arrayref && @$arrayref) { # do something with @$arrayref like: for my $i (@$arrayref) { print $i; } }
This returns: "arrayref not defined ref=ARRAY" But if I change this line:#!/usr/bin/perl use strict; my $arrayref = []; if(defined @$arrayref) { print "arrayref defined\n"; } else { my $ref = ref $arrayref; print "arrayref not defined ref=$ref\n"; }
tomy $arrayref = [];
I get the same result. So the question is, does: if(defined @$arrayref) actually test for definition of the array reference in such a way that you could then perform operations on @$arrayref from within the block without crashing Perl?my $arrayref;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing for definition of references
by fglock (Vicar) on Jun 26, 2003 at 18:03 UTC | |
by DBX (Pilgrim) on Jun 26, 2003 at 18:09 UTC | |
by Anonymous Monk on Jun 26, 2003 at 18:15 UTC | |
by fglock (Vicar) on Jun 26, 2003 at 18:18 UTC | |
|
Re: Testing for definition of references
by jsprat (Curate) on Jun 26, 2003 at 18:10 UTC | |
by DBX (Pilgrim) on Jun 26, 2003 at 18:18 UTC | |
by jsprat (Curate) on Jun 26, 2003 at 18:25 UTC |