Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1181478]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2018-01-21 16:42 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.