http://www.perlmonks.org?node_id=1220165

LanX has asked for the wisdom of the Perl Monks concerning the following question:

hi

I want to find out if a function is called with an integer or a string containing an integer.

Perl tries to hide what the scalar internally is, but Devel::Peek can be used here, the flag IOK shows an integer, POK a string.

looks_like_number() is no help here, I want the exact opposite.

Is there a more elegant way than catching and parsing the output of Dump?

NB: Using PerlGuts optree vodoo is considered helpful but not more elegant ... ;)

use strict; use warnings; use Data::Dump qw/pp dd/; use Scalar::Util qw/looks_like_number/; use Devel::Peek; sub is_string { Dump($_[0]); warn "$_[0] looks_like_number\n\n\n" if looks_like_number($_[0]); } is_string(1); is_string("42"); my $n=1; my $s="42"; is_string($n); is_string($s); $n.=""; # cast to string $s+=0; # cast to num is_string($n); is_string($s);

SV = IV(0x228e98) at 0x228ea8 REFCNT = 1 FLAGS = (PADTMP,IOK,READONLY,pIOK) IV = 1 1 looks_like_number SV = PV(0x24d378) at 0x245af8 REFCNT = 1 FLAGS = (PADTMP,POK,READONLY,pPOK) PV = 0x22e078 "42"\0 CUR = 2 LEN = 16 42 looks_like_number SV = IV(0x28ae40) at 0x28ae50 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 1 1 looks_like_number SV = PV(0x3bd068) at 0x28a6a0 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) PV = 0x26a1078 "42"\0 CUR = 2 LEN = 16 42 looks_like_number SV = PVIV(0x234c40) at 0x28ae50 REFCNT = 1 FLAGS = (PADMY,POK,pPOK) IV = 1 PV = 0x26a1048 "1"\0 CUR = 1 LEN = 16 1 looks_like_number SV = PVIV(0x234c58) at 0x28a6a0 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 42 PV = 0x26a1078 "42"\0 CUR = 2 LEN = 16 42 looks_like_number

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

) which involves redirecting STDERR ... Yuck!!!