Search code examples
stringperltypesinteger

How to tell apart numeric scalars and string scalars in Perl?


Perl usually converts numeric to string values and vice versa transparently. Yet there must be something which allows e.g. Data::Dumper to discriminate between both, as in this example:

use Data::Dumper;
print Dumper('1', 1);

# output:
$VAR1 = '1';
$VAR2 = 1;

Is there a Perl function which allows me to discriminate in a similar way whether a scalar's value is stored as number or as string?


Solution

  • There's no way to find this out using pure perl. Data::Dumper uses a C library to achieve it. If forced to use Perl it doesn't discriminate strings from numbers if they look like decimal numbers.

    use Data::Dumper;
    $Data::Dumper::Useperl = 1;
    print Dumper(['1',1])."\n";
    
    #output
    $VAR1 = [
              1,
              1
            ];