http://www.perlmonks.org?node_id=758106


in reply to Re: What is true and false in Perl?
in thread What is true and false in Perl?

Why is the second case unexpected?

"0 but true" is not "True".

It means "treat me in numeric context as 0, but in boolean context as true".

So when you do "0 but true" + 0 you are in numeric context, so you effectively do 0+0 which is 0 and this in turn (for the ? operator) is false.

I find that quite logical (which probably shows that using Perl for too long causes brain-damage).

And by the way: You are not doing numeric addition on a string, you are evaluating "0 but true" in numeric context (to 0) - therefore no warning.

Replies are listed 'Best First'.
Re^3: What is true and false in Perl?
by ikegami (Patriarch) on Sep 15, 2009 at 17:53 UTC

    And by the way: You are not doing numeric addition on a string, you are evaluating "0 but true" in numeric context (to 0) - therefore no warning.

    That makes no sense. The correct answer is that "0 but true" is special-cased.

    $ perl -wle'print 0+"0 abc"' Argument "0 abc" isn't numeric in addition (+) at -e line 1. 0 $ perl -wle'print 0+"0 but true"' 0
      Only in the context of perl -e, as when run from a script, generates the same error (Argument "0 but true" isn't a numeric in addition (+) at line #. Also, both still print 0. And in both cases the attempt to add the string to 0 is ignored.

        Not quite sure what you are saying, but neither is behavior different when run as a 'script' versus on the command line via -e, nor is the arithmetic operation 'ignored'.

        >cat 0_but.pl use warnings; use strict; print 42 * '0 but true', "\n"; print 42 * '0 but zonk', "\n"; >perl 0_but.pl 0 Argument "0 but zonk" isn't numeric in multiplication (*) at 0_but.pl +line 5. 0

        Update: Verfied on ActiveState 5.8.9 and Strawberries 5.10.1, 5.12.3 and 5.14.2.