|Do you know where your variables are?|
Reliably parsing an integerby rdiez (Acolyte)
|on Jun 29, 2020 at 14:04 UTC||Need Help??|
rdiez has asked for the wisdom of the Perl Monks concerning the following question:
I am writing the best Perl script since sliced bread:https://github.com/rdiez/Tools/tree/master/RDChecksum
But I have other, equally super scripts around. Top of the pops.
Anyway, I do not really trust my pesky users. Some of them have been known to pass some rubbish in command-line option --resume-from-line, instead of an integer. You know, something like --resume-from-line='from yesterday, you know what I mean'. The script is also reading a file which contains file sizes, and those sizes should be integers too. But who knows what the script might encounter there.
So I decided to do some validation, like you do in C++ or in Java: read as string, convert it to integer, and if that fails, tell the user: "Invalid integer. Sod off."
I was also hoping that, if the number is a real integer, Perl will run faster. In any case, I need to write those values to a file, so the output must look like a valid integer too.
Alas, I am finding it difficult to do a simple integer validation in Perl. The first thing I did is to write routine has_non_digits() in order to discard anything that is obviously not an integer, see the script I mentioned above. But it looks like that is not enough. If you try the following test code:
my $str = "99999999999999999999"; print "What the string is: $str\n"; my $strAsInteger = int( $str ); print "Value of \$strAsInteger: $strAsInteger\n"; printf "How printf sees it: %u\n", $strAsInteger;
You will get this result:
What the string is: 99999999999999999999 Value of $strAsInteger: 1e+20 How printf sees it: 18446744073709551615
The results are puzzling. Note that there is no sign of a hint, a warning, or anything helpful there.
Now, let's assume for a moment that anybody of us gives a damn about writing perfect Perl scripts. What would be the best way to parse an integer?
Because my script is not doing any arithmetic, I would like to accept integers up to the maximum integer limit. This particular script is fussy and only wants to run on Perl versions compiled with 64-bit integer support, but I have other scripts that are not so choosy, so the integer size might be 32 bits. In any case, the maximum value is not a round base-10 value like "9999".
In the name of all users that have the privilege of running my fine scripts, I thank you all.