You may be able to exploit the ~ operator, it has deep knowledge of Perl's internals. For example:
$string = "1"; $integer = 1; print ~$string; # Prints an extended character print ~$integer; # Prints 4294967294
Much easier than messing with perlguts. But there's one caveat: using ~ on a string with extended characters results in ASCII characters; that is ~chr(205) is "2". Assuming your definition of "string" does not include extended characters (only ASCII), you should be able to get away with a function like this:
sub is_integer { return (~$_[0] =~ m/^[0-9]+$/) ? 1 : 0; } print "1 ", is_integer(1), "\n"; # 1 ok print "'1' ", is_integer('1'), "\n"; # 0 ok print "chr(205) ", is_integer(chr(205)),"\n"; # 1 not ok
That said, be careful with this power. You don't want to confuse your users. Problems occur when reading data from files or console, as everything read is a string unless explically changed via +0. This also doesn't work for floating point. But sometimes it may have useful uses.

Hope this helps,

Jeff Connelly

In reply to Re: 1 vs "1" by Jeff Connelly
in thread 1 vs "1" by Superlman

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.