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

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?

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

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