in reply to Re: Determining whether a value is zero.
in thread Determining whether a value is zero.

It works only if I do not touch (i.e. expose it to various contexts) the variable before running the test:
for my $x (0, "0", 0.0, dualvar(0,1), dualvar(0,"0 "), '----------------', 1, "foo", "00", "0 ", undef, "", dualvar(1,0), "0.0", ) { print "$x before: ",is_zero($x),"\n"; my $y = ($x+1.1).$x; print "$x after: ",is_zero($x),"\n"; }

Replies are listed 'Best First'.
Re^3: Determining whether a value is zero.
by ikegami (Patriarch) on Mar 11, 2011 at 08:56 UTC

    It works as per the OP's spec. Don't blame me for his spec being awful!

    For strings,

    0+$x; #Upd: Or 1.1 or whatever

    is effectively

    $x = dualvar(0+$x, $x) if looks_like_number($x);

    The OP was clear that dualvar(0, $anything) should be considered zero.

      dualvar 0,"0 " is considered zero after the assignment to $y in my code. That is against the specification I fear...
      UPDATE: corrected.
        No. In fact, after your initial reply, I particular pointed out that your first code incorrectly considers dualvar 0, "0 " to be not be a zero.

        dualvar 0, ANYTHING should be considered a zero.

        "0 " is considered zero after the assignment to $y in my code

        You never test "0 " after the assignment to $y. You test dualvar(0, "0 "). Please re-read my post.

Re^3: Determining whether a value is zero.
by JavaFan (Canon) on Mar 11, 2011 at 11:08 UTC
    It works only if I do not touch (i.e. expose it to various contexts) the variable before running the test:
    Well, yes. That's because looking at variables in Perl can actually modify them.