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

Re: Distiguishing arguments: number-strings vs real integer

by Eily (Monsignor)
on Aug 10, 2018 at 12:29 UTC ( #1220187=note: print w/replies, xml ) Need Help??


in reply to Distiguishing arguments: number-strings vs real integer

Another solution, which IMHO is a little more elegant than having to parse the output of another module: Scalar::Util's isdual() will return true for a scalar that has both a number and string representation. As documented, this is true for a number used in string context and the reverse for a string.

So a scalar is a number if it becomes dual after stringification, and is a string if it becomes dual after numification (and is a trap if already dual to begin with ;-) ).

perl -MScalar::Util=isdual -E "@v = (0, 1, 2, '3', '4', '5'); push @v, + @v; push @v, $v[0].$v[3]; push @v, $v[1]+$v[4]; isdual($_) and say f +or @v" 0 4
The push @v, @v is to check that a variable's copy is left unaffected by the stringification/numification of the original (yes it's a very weak and useless check :P). After that, I stringify $v[0] and numify $v[4]. (The values 2, and 5 are unused so not dual, 1 and 3 are used as number and string respectively, which they already were)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1220187]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2020-04-01 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    To "Disagree to disagree" means to:









    Results (186 votes). Check out past polls.

    Notices?