in reply to (tye)Re: forcing numeric context
in thread forcing numeric context

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;.

Replies are listed 'Best First'.
(tye)Re2: forcing numeric context
by tye (Sage) on Dec 18, 2002 at 15:14 UTC

    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
      Isn't !1 just an expression that returns 0? No, trying it shows blank. It seems to be a zero-length string. So why is it different from ''? Is it magic?

        !1 returns Perl's "false" value. You can get the same effect via:

        sub false { my $f= ""; { BEGIN { warnings->unimport() if $INC{"warnings.pm"}; } local( $^W )= 0; my $temp= 0+$f; } return $f; }
        That is, the "false" value simply has a string value of "" and a numeric value of 0. Thus there is no warning when in numeric context since the 0 is simply fetched rather that it having to be computed from the empty string.

                - tye