Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi all:

I am writing the best Perl script since sliced bread:

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.

In reply to 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 rifling through the Monastery: (11)
    As of 2021-03-02 14:05 GMT
    Find Nodes?
      Voting Booth?
      My favorite kind of desktop background is:

      Results (49 votes). Check out past polls.