Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: detecting of scalar is string or numeric

by syphilis (Chancellor)
on Feb 08, 2017 at 23:36 UTC ( #1181478=note: print w/replies, xml ) Need Help??


in reply to detecting of scalar is string or numeric

if ($x ^ $x) {print "string\n";}

Seems to me that this will print "string" when and only when neither the NOK or IOK flag is set.
That being so, an alternative is to check whether the IOK or NOK flag is set - for which a module probably exists, or you could do it via XS or Inline::C.
use strict; use warnings; use Inline C => Config => BUILD_NOISY => 1, ; use Inline C => <<'END_C'; int foo(SV * in) { if(SvIOK(in) || SvNOK(in)) return 1; return 0; } END_C my $num = 42; my $str = "42"; my $ref = \$num; my $nv = 42.42; my $dual = "42"; # POK flag set. if($dual > 0){} # IOK flag also set print foo($num), " ", xor_check($num), "\n"; print foo($str), " ", xor_check($str), "\n"; print foo($ref), " ", xor_check($ref), "\n"; print foo($nv) , " ", xor_check($nv) , "\n"; print foo($dual), " ", xor_check($dual), "\n"; sub xor_check { return 0 if($_[0] ^ $_[0]); return 1; } __END__ Outputs (perl-5.24.0): 1 1 0 0 0 0 1 1 1 1
Now, you might be able to find a variable for which foo() and xor_check() return different values.
I'm certainly interested to know if someone can provide such an example.

Cheers,
Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1181478]
help
Chatterbox?
talexb finishes writing a Cranky Old Man post, and hopes it will be taken in the spirit intended. :)
[Corion]: We're happy to have you here, talexb!
[talexb]: Thanks. :)

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-07-19 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (409 votes). Check out past polls.

    Notices?