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 (Saint) 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 (Monsignor) 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