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 |