I was looking at the code for Scalar::Util, and in specific at the source for the looks_like_number() function. There's something strange in the code that prompts me to ask the question of 'why'.

Here is the source for the looks_like_number() function, pasted from Scalar::Util:

sub looks_like_number { local $_ = shift; # checks from perlfaq4 return $] < 5.009002 unless defined; return 1 if (/^[+-]?\d+$/); # is a +/- integer return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float return 1 if ( $] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i ); 0; }

The line I'm questioning is:

return $] < 5.009002 unless defined;

As I understand from reading perlvar, $] contains the version + patchlevel / 1000 of the perl interpreter. The question is why is Scalar::Util's looks_like_number() written to return truth if its parameter is undefined and the version is less than 5.009002? This would mean that the looks_like_number() function will errantly report an undefined value as looking like a number if you're using a sufficiently old version of Perl5. Is this really what is intended? Or am I missing something simple?


Dave


In reply to Scalar::Util 'looks_like_number()' code's $] comparison by davido

Title:
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, details, 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, summary, 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.