Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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!!!


In reply to Distiguishing arguments: number-strings vs real integer by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2024-04-18 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found