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 endIn 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:
- being equal to 0
- being equal to the empty string, ''
Best, beth
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: "defined" in while loop Edit | Delete | Quote | Reply | Private Reply
by ikegami (Patriarch) on Feb 16, 2009 at 23:47 UTC | |
by ELISHEVA (Prior) on Feb 17, 2009 at 00:34 UTC | |
by ikegami (Patriarch) on Feb 22, 2009 at 23:55 UTC |
In Section
Seekers of Perl Wisdom