in reply to Idiomatic Perl

Anonymous Monk,
This can be trickier than it first appears due to the fact that $increment may exist but not be numeric - pesky warnings and strictures. You also run into a problem if the $increment is not defined since looks_like_number says that an undefined value looks numeric.
#!/usr/bin/perl use strict; use warnings; use Scalar::Util 1.10 'looks_like_number'; my $total = 4; my $increment = 'blah'; $total += $increment || 0 if looks_like_number( $increment );
Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Idiomatic Perl
by flyingmoose (Priest) on Mar 02, 2004 at 20:12 UTC
    In the spirit of TIMTOWTDI and my well-beloved ternary abuse (which I like since many other languages have it):

    $total += looks_like_number($increment) ? $increment : 0
      flyingmoose,
      This won't work for the reason I described above. If $increment is undefined looks_like_number will still return true.
      #!/usr/bin/perl use strict; use warnings; use Scalar::Util 1.10 'looks_like_number'; my $total = 4; my $increment; $total += looks_like_number($increment) ? $increment : 0 __END__ Use of uninitialized value in addition (+) at blah.pl line 9.
      Cheers - L~R
        Fair enough. That was typed in the middle of a boring telecom (as are most of my posts), so that explains my inability to read.