Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
How is a regular expression going to help me accept 4294967295 (UINT32_MAX) but not 4294967296 (UINT32_MAX+1)?

AnomalousMonk answered this one; I suggested the module because it's good for verifying various number formats.

I would rather avoid the overhead of using Math::BigInt if I can.

Well, if you want to go right up to that limit*, then I think the safest way is to use Math::BigInt, because it'll correctly handle strings that are clearly over the limit. It's a core module and it's not really that much overhead: once you've used the module to confirm that the number will work as a normal integer without loss of precision, you no longer need the object and can just work with a plain Perl scalar afterwards. Just an example:

use warnings; use strict; use feature 'state'; use Carp; use Math::BigInt; use Config; use Regexp::Common qw/number/; sub validate_int { my $str = shift; state $max = Math::BigInt->new( eval $Config{nv_overflows_integers_at} ); croak "not an integer" unless defined $str && $str=~/\A$RE{num}{int}\z/; my $num = Math::BigInt->new($str); croak "integer to small" if $num < 0; croak "integer too big" if $num > $max; return $num->numify; } use Test::More; sub exception (&) { eval { shift->(); 1 } ? undef : ($@ || die) } is validate_int(0), 0; is validate_int(1), 1; is validate_int(3), 3; ok exception { validate_int(undef) }; ok exception { validate_int("") }; ok exception { validate_int("x") }; ok exception { validate_int("123y") }; ok exception { validate_int(-1) }; ok exception { validate_int("-9999999999999999999999999999999") }; my $x = Math::BigInt->new(eval $Config{nv_overflows_integers_at})-1; is validate_int("$x"), 0+$x->numify, "'$x' works (max-1)"; $x++; is validate_int("$x"), 0+$x->numify, "'$x' works (max)"; $x++; ok exception { validate_int("$x") }, "'$x' fails (max+1)"; ok exception { validate_int("999999999999999999999999999999999999") }; done_testing;

* Update: I named several integer limits in the post I linked to. Depending on which of those limits you want to use, hippo's suggestion from here is of course much easier.

In reply to Re^3: Reliably parsing an integer (updated) by haukex
in thread Reliably parsing an integer by rdiez

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2020-10-29 17:30 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (274 votes). Check out past polls.