John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

I have a value that's blank, and I want it to show as the numeral zero. If I just use $v in the interpolation, it naturally just comes out blank. If I say +$v in an expression (say an argument to print or join, not within a string literal), I thought it would force numeric context and give a zero. It doesn't do anything at all! If I try 0+$v, I get the expected result, but also get a warning that "" is not a number in the case where it was blank.

Why doesn't the leading + work? I thought that was supposed to do it.

FWIW, printf with %d and int work OK, but give warnings too.

Replies are listed 'Best First'.
(tye)Re: forcing numeric context
by tye (Sage) on Dec 17, 2002 at 22:46 UTC

    This is why I use !1 as my "false" value. (And I use 0+ to force numeric context.)

            - tye
      The 0+ works but causes a warning. I went with $v||0 as the simplest in this case. I think I said something like $s=join ',', map { $_||0 } @version;.

        Just to make sure you didn't miss the point. If you use $f= !1 then 0+$f does not cause a warning. So, if you want $f to stay the empty string, you can also use:     $f ||= !1; if you don't have control over how $f is initially set. Then 0+ and sprintf "%d", etc. will all work without giving a warning.

                - tye
Re: forcing numeric context
by Anonymous Monk on Dec 17, 2002 at 22:45 UTC

    Quoting from perlop:

    Unary "+" has no effect whatsoever, even on strings. It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments.


    Fun Fun Fun in the Fluffy Chair

Re: forcing numeric context
by tachyon (Chancellor) on Dec 17, 2002 at 22:48 UTC

    If you want it to be zero why not just make it so. If will still eval as false in conditionals...

    use warnings; my $v = ''; $v ||= 0; print "\$v = '$v'";

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Re: If you want it to be zero why not just make it so.

      This is a version tuple for a C++ DLLs and EXEs. In one place, it is omitted if the "patch" value is not used, and that is the way it appears in the GUI. In another place, I need all 4 numbers separated with commas, regardless.

      This is a program that takes release information via a nice user-friendly form, and updates the directory tree, changing all the places the version appears in files. If the program wasn't already using an empty string here (other code formats it different ways in different uses), I'd probably just use numbers consistantly.

      So the short answer: maintainance often contradicts good design.

      —John

Re: forcing numeric context
by dpuu (Chaplain) on Dec 17, 2002 at 22:48 UTC
    One way to avoid the warning is to use "$v || 0". I'm not sure why "+$v" doesn't work: perhaps its a new feature of Perl6, not in Perl5. --Dave