Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: How to check if a scalar value is numeric or string?

by Anonymous Monk
on Feb 07, 2017 at 19:42 UTC ( [id://1181335]=note: print w/replies, xml ) Need Help??


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

As of perl 5.24 you can no longer use this trick because ^ does not like strings with code points over 0xFF. Right now it is deprecated, but will presumably be fatal later.
  • Comment on Re^3: How to check if a scalar value is numeric or string?

Replies are listed 'Best First'.
Re^4: How to check if a scalar value is numeric or string?
by voidzero (Initiate) on Jul 18, 2023 at 08:01 UTC

    Hi, this is day 2 of my perl journey and I'm doing some exercises in Learning Perl. Where can I read more on what ^ was, why it was deprecated, and on $x & ~$x?

      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."


      🦛

      Sorry for being harsh, but on day 2 of your Perl journey it is a waste of time to examine why some function was deprecated.

      You can read about the operators & at Bitwise And and about ^ at Bitwise Exclusive Or, both in perlop. The same document also has a section on Bitwise String Operators which explains some of the caveats, and if you really, really want to know you can consult perldeprecation to learn more about what has been deprecated.

      > Hi, this is day 2 of my perl journey and I'm doing some exercises in Learning Perl

      It's also day 1 of your Perl Monks journey, and you've learnt a valuable lesson: asking us a brand new question is way better than responding to a random old post. :)

      Since you're learning Perl, please take a look at Learning Perl Links and feel free to let us know which links you found the most useful.

      Oh, and when you hit a problem during your Perl learning, do ask us a new question in Seekers of Perl Wisdom and we will be glad to help!

      Here are some links to help get you started at Perl Monks:

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found