cajwine
cajwine

Reputation: 3160

Differentiate string and number argument in perl

How to solve the following?

use 5.014;
use warnings;
use Test::Simple tests => 4;

ok( doit(0123)   == 83, "arg as octal number" );
ok( doit(83)     == 83, "arg as decimal number" );
ok( doit('0123') == 83, "arg as string with leading zero" );
ok( doit('123')  == 83, "arg as string without leading zero" );

sub doit {
    my $x = shift;
    return $x;                                     # how to replace this line
    #return  got_the_arg_as_string ? oct($x) : $x; # with something like this
}

E.g. If i pass to the doit sub any string - mean quoted value - (with or without the leading zero), it should be converted to octal value. Otherwise, it is just an number.

Upvotes: 2

Views: 431

Answers (1)

mob
mob

Reputation: 118665

Perl's internal representation of a scalar may be as in integer or a string, and it remains ready to coerce that representation into any other scalar type at any moment. It is possible with C/XS code to get at a scalar's internal type. The JSON::XS module does this, for example, to decide whether a value should be rendered as a number or as a string.

Here's a proof of concept for your problem:

use Inline 'C';
sub foo {
    my ($x) = @_;
    print $x, " => isString: ", isString($x), "\n";
}
foo(0123);
foo('0123');

__END__
int isString(SV* sv)
{
    return SvPOK(sv) ? 1 : 0;
}

Program output:

83 => isString: 0
0123 => isString: 1

Related posts:

Difference between $var = 500 and $var = '500'

When does the difference between a string and a number matter in Perl 5?

Why does the JSON module quote some numbers but not others?

Update some of this functionality is exposed in the core B module, so no need to add as XS dependency:

use B;
sub isString {
    my $scalar = shift;
    return 0 != (B::svref_2object(\$scalar)->FLAGS & B::SVf_POK)
}

Upvotes: 7

Related Questions