On Tue, Mar 29, 2011 at 22:03, Jim Gibson <jimsgib...@gmail.com> wrote:
snip
> I have heard this advice before, and it just sounds silly to me.
> I deal exclusively with ASCII characters, so \d will only match
> the characters '0' through '9'.
snip

If you are dealing exclusively with ASCII, then you should be using
the [bytes][0] pragma; however, just because you are dealing with only
ASCII today doesn't mean you will always be dealing only with ASCII.
At some point in the near future you will probably have to start
dealing with Unicode.  When that happens (and it really is a matter of
when, not if), do you want to go through all of your code and fix the
problems you could have easily avoided?

snip
> Can you provide any example of a situation where something bad
> will happen from using \d?
snip

Simple.  In the case we are talking about things will got terribly
astray if you happened to match something that wasn't [0-9]:

#!/usr/bin/perl

use strict;
use warnings;

binmode STDOUT, ":utf8";

my $s = "\x{ff15}.\x{ff15}.\x{ff15}.\x{ff15}";

print "$s\n";
$s =~ s/(\d+)[.](\d+)[.](\d+)[.](\d+)/sprintf "%x.%x.%x.%x", $1, $2, $3, $4/e;
print "$s\n";

5.5.5.5
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
Argument "\x{ff15}" isn't numeric in sprintf at w.pl line 11.
0.0.0.0

The problem is that things that match \d just don't behave like
numbers.  Only ten of the hundreds of digits characters are actually
numbers.  They may look like numbers, as the Fullwidth Digit Five
characters I used here do, but Perl 5 still treats them like
non-number characters.

snip
> The statement "To match the ASCII digit characters you must use [0-9]" is
> wrong. I believe you meant to say "To match the ASCII digit characters and
> only those characters, you must use [0-9]".
snip

By your logic, it is perfectly fine to use . to match numbers.  After
all, . will match [0-9].  If you have bothered to specify \d, you most
likely mean [0-9].

[0]: http://perldoc.perl.org/bytes.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to