[oak perl] The mysterious "undef"
dbkliv
dbkliv at gmail.com
Fri Mar 18 12:36:10 PST 2005
On Fri, 18 Mar 2005 12:55:46 -0500, Sandy Santra <santranyc at yahoo.com> wrote:
> For this, doesn't the "my" have to be outside the loop?
>
> When I run:
>
> use strict;
> sub test {
> my ( $x, $y, $z ) = @_;
> }
> test(1,0);
> print "$x\n";
> print "$y\n";
> print "$z\n";
> print "x is undef\n" if not defined $x;
> print "y is undef\n" if not defined $y;
> print "z is undef\n" if not defined $z;
>
> I get compilation errors (i.e., "Global symbol "$x" requires explicit
> package name at test2 line 7").
Right - because $x, $y, and $z are scoped to test().
Convert your code to:
#!/usr/bin/perl
use strict;
use warnings;
sub test {
my ( $x, $y, $z ) = @_;
print "$x\n";
print "$y\n";
print "$z\n";
print "x is undef\n" if not defined $x;
print "y is undef\n" if not defined $y;
print "z is undef\n" if not defined $z;
}
test(1,0);
__END__
1
0
Use of uninitialized value in concatenation (.) or string at - line 10.
z is undef
More information about the Oakland
mailing list