Re: When would you use qr// on a literal string?
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Mon, 17 Jul 2006 15:23:30 +0000 (UTC)
"it_says_BALLS_on_your forehead" <simon.chao@xxxxxxx> wrote in
news:1153145251.119197.122300@xxxxxxxxxxxxxxxxxxxxxxxxxxx:
I thought that qr was mainly used for pre-compiling variables into
regex patterns, but a colleague uses it like so:
my ( $name, $value ) = split qr/=/, $string;
Are there any benefits to doing this? He claims that the use of the
qr// op here can help capture an error if the STRING is not a valid
regex. Does this make any sense?
The first argument to split is a regex, whether you use regex notation or
not.
So, I am not fond of:
split '=', $string;
because it obscures that fact.
On the other hand, I don't see what additional mileage qr gets you other
than saying: I really want to signal that this is a regex to other
programmers.
As for what mileage that gets you in terms of preventing errors, take a
look at:
#!/usr/bin/perl
use strict;
use warnings;
my $data = 'name$sinan';
my @a = split '$', $data;
my @b = split /$/, $data;
my @c = split qr/$/, $data;
# correct
my @d = split /\$/, $data;
print "@a\n@b\n@c\n@d\n";
__END__
Sinan
.
- References:
- When would you use qr// on a literal string?
- From: it_says_BALLS_on_your forehead
- When would you use qr// on a literal string?
- Prev by Date: Re: Problem with ([\w ]+?)
- Next by Date: Re: why is perl -e 'unlink(glob("*"))' so much faster than rm ?
- Previous by thread: When would you use qr// on a literal string?
- Next by thread: Re: When would you use qr// on a literal string?
- Index(es):
Relevant Pages
|