[oak perl] The mysterious "undef"

Sandy Santra santranyc at yahoo.com
Fri Mar 18 09:55:46 PST 2005


At 09:22 AM 3/18/05 -0800, you wrote:

>$x = undef;
>print "x is undef\n" if not defined $x;
>$y = 0;
>print "y is defined\n" if defined $y;
>

Thanks!  Great piece of code.  Works great for testing.


>sub test {
>     my ( $x, $y, $z ) = @_;
>}
>
>with "test( 1, 2)", $x would be 1, $y would be 2, and $z would be
>undef (not zero).

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").

But this works fine:

use strict;
my ( $x, $y, $z );
sub test {
    ( $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;




--Sandy Santra



More information about the Oakland mailing list