in reply to Style and warnings

It is just a personal preference, but i would make a little sub that would package away the addition, and do the input checking (i.e. remove any non-digits, warn if the number is -1000_000, or any other of the situations pointed out) and provide helpful errors if anything is bad.

use warnings; ## or die! use strict; ## seriously - don't anger the gods use diagnostics; ## but if you do, make them tell you why (patiently) +... if (my $big_int = big_int('101_dalmations') ){ ... it was OK so go ahead and play ... } else { ... no need for this bit, error messages already generated! ... ... maybe you want to do something else though? ... } ### SUBS ### sub big_int{ my $input = shift; ## check for any source of errors if ( $input =~ /\D/ ){ ## non-digits - argh! warn "$input contains non digits\n"; return 0; } elsif (... something else ...){ ... } return ($input + 1000_000); }

It just means that what you are actually trying to achieve (make a big int) remains clear, while also handling the errors nicely if they don't conform etc...

Just MHO though...

Just a something something...

Replies are listed 'Best First'.
Re^2: Style and warnings
by JavaFan (Canon) on Aug 18, 2009 at 15:43 UTC
    There are perfectly valid numbers that Perl can deal with, even without warnings, which still contain non-digit characters. 1.3 comes to mind. And so do -5 and 3e5.

    Furthermore, there are strings containing no \D characters, but which Perl cannot treat as numbers. "๑๒๓" for instance.

    If you wish to find out whether a string looks like a number, use Scalar::Util::looks_like_number. Don't use simplistic regexes.

      Just making an example. OP would have to make specific checks based on their input.

      You never know, maybe they are only dealing with integers, possibly joined with types of dog, in which case my example was perfect.

      Just a something something...
        You never know, maybe they are only dealing with integers, possibly joined with types of dog, in which case my example was perfect.
        Hmmm. Well, in that case, suppose the OP was dealing with a $seqnum equal to -9999997, in which case
        my $bigseqnum = 3;
        would have been perfect! You never know!
Re^2: Style and warnings
by api (Initiate) on Aug 18, 2009 at 17:39 UTC
    Thanks all very much for the great answers!!!
    A subroutine to allow for using the leading number and logging a warning with blocking the consul log seems best for this case.

    Thanks!!