in reply to Warning-free numerification of a string

for ($val1, $val2) { $_--; $_++; }

works, at least for most values, but

for ($val1, $val2) { no warnings 'numeric'; $_ += 0; }

is far less riskier and far more readable/maintainable.

Replies are listed 'Best First'.
Re^2: Warning-free numerification of a string
by oxone (Friar) on Aug 08, 2007 at 19:21 UTC
    Thanks! Very sneaky, I like it. Probably a bit too sneaky to be considered maintainable, so I'll run with the latter. At least now I know I'm not missing a neat trick here.
      If $_ is a string that that does not look_like_number(), then don't be fooled into thinking that $_ + 0 will inevitably evaluate to zero:
      $_ = '12*&&#$%'; $_ += 0; print $_, "\n"; # prints 12
      Cheers,
      Rob
      Not sneaky at all, just contain the no-warnings to where you need it and the requirement is crystal-clear:
      { no warnings 'numeric'; $val1 = $val1+0; # force numeric }