http://qs321.pair.com?node_id=744237


in reply to "defined" in while loop Edit | Delete | Quote | Reply | Private Reply

OP writes:

1) while(<stdin>){print " I saw $_" ;} # Is thie (sic) equivalent to 2) while($_=<stdin>){ print "I saw $_" ;} so how will while loop end because undefined value in the $_ will not let the loop end

In Perl, the assignment $_ = <> doesn't necessarily make $_ "defined"? Strange isn't it? But here is how it happens:
my $x = undef; #clears out $x and makes it undefined my $y = $x; #since $x is undefined, so is $y

It might be easier to think of "undefined" in Perl as "null" rather than "not yet assigned".

Also, it isn't necessary for $_ to be undefined (or unassigned) for the loop to end. A while loop ends when expr in while (expr) evaluates to false. Being undefined is only one way of being false. Other ways of being false are:

Best, beth