Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: truth in while condition (updated)

by AnomalousMonk (Chancellor)
on Sep 30, 2018 at 18:40 UTC ( #1223335=note: print w/replies, xml ) Need Help??


in reply to truth in while condition

Truthiness is also discussed in Truth and Falsehood in perlsyn. As has been pointed out by others, you were testing "0\n" in the while-loop conditional and not the truly false string "0".

Update: At last, I see the point first made by LanX here: even if  <STDIN> managed to return a plain, nominally false  "0" character, the loop would still execute because  "0" is defined and the special-case while-loop is conditional upon defined-ness! Thanks to BillKSmith (here) and LanX (here) for helping the scales to fall from my eyes.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: truth in while condition
by LanX (Archbishop) on Sep 30, 2018 at 21:55 UTC
    > you were testing "0\n" in the while-loop conditional and not the truly false string "0".

    The following iterator returns no "\n" but still fails to stop at "truly false" number 0.

    see explanation here

    use strict; use warnings; use feature 'say'; my $i = Iterator->new([1,0,2,undef,3]); while ( <$i> ) { say "<$_>"; } package Iterator; use overload '<>' => sub { shift @{$_[0]} }; sub new { my ($class,$obj) =@_; bless $obj, $class; }

    -->

    <1> <0> <2>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: truth in while condition
by BillKSmith (Parson) on Sep 30, 2018 at 21:25 UTC
    The reference in the first reply LanX explains that in this case, 'while' tests 'definedness' not 'Truthiness'.
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1223335]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2019-07-19 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?