in reply to Question re numbers and strings

When you use a variable as a number, Perl tries hard to make it a number if it's not stored as a number internally. In Perl it's the operator that provides the context, and the data is transformed to fit to the operation.

In the case of "1abc" the result is 1, plus a warning (if you have warnings enabled, as I strongly recommend). So it is indeed a number, if viewed as one.

The test you might want to use instead is implemented in Scalar::Util::looks_like_number

Replies are listed 'Best First'.
Re^2: Question re numbers and strings
by ultranerds (Hermit) on Jan 13, 2011 at 09:06 UTC
    Hi,

    Thanks for the reply guys - thats sheds some light on the matter :)

    I think what I will do, is just use a regex for now then:

    $var =~ /^[1-9]\d*$/

    (the first bit is just to make sure its over 0 :))

    Thanks again

    Andy
      Just a tangential word of caution, Andy; quoting (determining interpolation) matters:
      C:>perl -e "my $var = '0x8';if ($var =~ /^[1-9]\d*$/) {print 'num' } e +lse {print 'not a num';}" not a num

      But

      C:>perl -e "my $var = 0x8;if ($var =~ /^[1-9]\d*$/) {print 'num' } els +e {print 'not a num';}" num