in reply to Number too big to fit in integer

Is there a simpler way to ensure arguments fit in integers?

You get the arguments as strings from @ARGV, and as long as you don't treat them as numbers, Perl will keep them as strings, allowing you to use string comparison operations:

use warnings; use strict; my $x = shift; length($x) && $x=~/\A[0-9]+\z/ or die "Need a non-negative integer"; my $y = "1000000000000000000000000000000000000000"; # just for demo die "Integer too big" if length($x)>length($y) or sprintf("%0*s", length($y), $x) gt $y; print "$x is ok\n"; # *now* you can treat $x like a number

Replies are listed 'Best First'.
Re^2: Number too big to fit in integer
by jpl (Monk) on Dec 25, 2022 at 16:56 UTC

    Might want to trim leading 0s off the argument, being careful not to trim a simple 0 to the empty string. And Perl will do the "right thing" with a leading +

    DB<175> $av0="+10" DB<176> say sprintf("%u", $av0) 10

    I don't want to anticipate all the stuff Perl might do to an argument on the way to treating it as a number. I think Math::BigInt may be the (conceptually) simplest thing.