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
°) which involves redirecting STDERR ... Yuck!!!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.