Differentiate string and number argument in perl

cajwine

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.

mob

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)
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Number and string concatenation in Perl

From Dev

TypeError: float() argument must be a string or a number in Django distance

From Dev

Differentiate between String and [Char]

From Dev

Perl module to transform string number 2.4E+04 to number

From Dev

int() argument must be a string or a number, not 'SimpleLazyObject'

From Dev

Django TypeError int() argument must be a string or a number, not 'QueryDict'

From Dev

Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable'

From Dev

perl-how to treat a string as a binary number?

From Dev

Regex to differentiate String keys

From Dev

saving datetimefiled in django models, int() argument must be a string or a number

From Dev

Python: Using one argument to handle choice between a number and a string?

From Dev

Why does perl's unpack() think that the second argument is a string?

From Dev

int() argument must be a string or a number, not 'builtin_function_or_method' - python

From Dev

perl number of lines in a string

From Dev

TypeError: int() argument must be a string or a number, not 'Model Instance'

From Dev

TypeError: int() argument must be a string or a number, not 'datetime.datetime'

From Dev

Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any'

From Dev

argument of type 'string' is not assignable to parameter of type 'number' typescript in visual studio

From Dev

TypeError: Float() argument must be a string or a number when reading a list()

From Dev

Converting string to number “Argument isn’t number error”

From Dev

int() argument must be a string, a bytes-like object or a number, not 'object'

From Dev

TypeError: float() argument must be a string or a number, not 'Tensor'

From Dev

int() argument must be a string or a number

From Dev

saving datetimefiled in django models, int() argument must be a string or a number

From Dev

Python: Using one argument to handle choice between a number and a string?

From Dev

int() argument must be a string, a bytes-like object or a number, not 'method'

From Dev

Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any'

From Dev

int() argument must be a string or a number, not 'SimpleLazyObject' in django

From Dev

TypeError: float() argument must be a string or a number, not 'IntVar'

Related Related

  1. 1

    Number and string concatenation in Perl

  2. 2

    TypeError: float() argument must be a string or a number in Django distance

  3. 3

    Differentiate between String and [Char]

  4. 4

    Perl module to transform string number 2.4E+04 to number

  5. 5

    int() argument must be a string or a number, not 'SimpleLazyObject'

  6. 6

    Django TypeError int() argument must be a string or a number, not 'QueryDict'

  7. 7

    Django Error: TypeError: int() argument must be a string or a number, not 'BuildsTable'

  8. 8

    perl-how to treat a string as a binary number?

  9. 9

    Regex to differentiate String keys

  10. 10

    saving datetimefiled in django models, int() argument must be a string or a number

  11. 11

    Python: Using one argument to handle choice between a number and a string?

  12. 12

    Why does perl's unpack() think that the second argument is a string?

  13. 13

    int() argument must be a string or a number, not 'builtin_function_or_method' - python

  14. 14

    perl number of lines in a string

  15. 15

    TypeError: int() argument must be a string or a number, not 'Model Instance'

  16. 16

    TypeError: int() argument must be a string or a number, not 'datetime.datetime'

  17. 17

    Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any'

  18. 18

    argument of type 'string' is not assignable to parameter of type 'number' typescript in visual studio

  19. 19

    TypeError: Float() argument must be a string or a number when reading a list()

  20. 20

    Converting string to number “Argument isn’t number error”

  21. 21

    int() argument must be a string, a bytes-like object or a number, not 'object'

  22. 22

    TypeError: float() argument must be a string or a number, not 'Tensor'

  23. 23

    int() argument must be a string or a number

  24. 24

    saving datetimefiled in django models, int() argument must be a string or a number

  25. 25

    Python: Using one argument to handle choice between a number and a string?

  26. 26

    int() argument must be a string, a bytes-like object or a number, not 'method'

  27. 27

    Typescript - An index expression argument must be of type 'string', 'number', 'symbol' or 'any'

  28. 28

    int() argument must be a string or a number, not 'SimpleLazyObject' in django

  29. 29

    TypeError: float() argument must be a string or a number, not 'IntVar'

HotTag

Archive