Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2020-03-31 03:13 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      To "Disagree to disagree" means to:









      Results (179 votes). Check out past polls.

      Notices?