in reply to Re^4: How to check if a scalar value is numeric or string?
in thread How to check if a scalar value is numeric or string?

Hello voidzero and welcome to the Monastery. Congratulations on beginning your Perl journey - many wonders await you!

Lots of the documentation is available in perldoc, both online and using the perldoc command in your terminal. The ^, & and ~ symbols in these cases are operators and you can read all about them in perlop.

Note that ^ is still a perfectly valid operator and not deprecated for general use. Our anonymous brother merely points out that the particular use case of $x ^ $x is not longer a valid method for determining the numerical nature of $x in the cases where $x is or may be a utf-8 string containing high code points. This is also true for the $x & ~$x method. For example this test:

use strict; use warnings; use Test::More tests => 6; my $x = 111; my $y = '111'; my $z = "1\N{LATIN CAPITAL LETTER GAMMA}1"; ok !($x & ~$x), '$x is a number, &~'; ok $y & ~$y, '$y is a string, &~'; ok $z & ~$z, '$z is a string, &~'; ok !($x ^ $x), '$x is a number, ^'; ok $y ^ $y, '$y is a string, ^'; ok $z ^ $z, '$z is a string, ^';

runs perfectly well in perl 5.22 and earlier. However, starting at perl 5.24 it produces warnings like these:

$ perl isnum.t 1..6 ok 1 - $x is a number, &~ ok 2 - $y is a string, &~ Use of strings with code points over 0xFF as arguments to 1's compleme +nt (~) operator is deprecated. This will be a fatal error in Perl 5.2 +8 at isnum.t line 12. Use of code point 0xFFFFFFFFFFFFFFCE is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 at isnum.t l +ine 12. Use of code point 0xFFFFFFFFFFFFFE6B is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 at isnum.t l +ine 12. Use of code point 0xFFFFFFFFFFFFFFCE is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 at isnum.t l +ine 12. Use of code point 0xFFFFFFFFFFFFFFCE is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 in bitwise a +nd (&) at isnum.t line 12. Use of strings with code points over 0xFF as arguments to bitwise and +(&) operator is deprecated. This will be a fatal error in Perl 5.28 a +t isnum.t line 12. Use of code point 0xFFFFFFFFFFFFFE6B is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 in bitwise a +nd (&) at isnum.t line 12. Use of code point 0xFFFFFFFFFFFFFFCE is deprecated; the permissible ma +x is 0x7FFFFFFFFFFFFFFF. This will be fatal in Perl 5.28 in bitwise a +nd (&) at isnum.t line 12. ok 3 - $z is a string, &~ ok 4 - $x is a number, ^ ok 5 - $y is a string, ^ Use of strings with code points over 0xFF as arguments to bitwise xor +(^) operator is deprecated. This will be a fatal error in Perl 5.28 a +t isnum.t line 16. ok 6 - $z is a string, ^

and in perl 5.28 or newer it errors out like this:

$ perl isnum.t 1..6 ok 1 - $x is a number, &~ ok 2 - $y is a string, &~ Use of strings with code points over 0xFF as arguments to 1's compleme +nt (~) operator is not allowed at isnum.t line 12. # Looks like your test exited with 255 just after 2.

So, don't rely on these bitwise methods where the arguments may be strings with high code points. Removing the 2 tests which operate on $z in this script allows it to run without errors or warnings in all environments.

FWIW I tend to agree with ig in Re^3: How to check if a scalar value is numeric or string? where he says "It might be better to revise your program so that it doesn't depend on such a subtle distinction."


🦛