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

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

Replies are listed 'Best First'.
Re: (tye)Re2: forcing numeric context
by John M. Dlugosz (Monsignor) on Dec 18, 2002 at 15:20 UTC
    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
        So, the numeric value is already cached in the value, so it doesn't generate a warning because it doesn't re-compute it. Right? Very interesting.