When would you use qr// on a literal string?

  • Thread starter it_says_BALLS_on_your forehead
  • Start date
I

it_says_BALLS_on_your forehead

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?
 
A

A. Sinan Unur

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
 
D

Dr.Ruud

it_says_BALLS_on_your forehead schreef:
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?

I don't see any difference in using /+/ or qr/+/, both give the same
error.
My gut feeling says that split does a precompile on a /PATTERN/.


$ perl -MO=Deparse -e 'print split ".+"'
print split(/.+/, $_, 0);

$ perl -wle 'print split "+"'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.

$ perl -wle 'print split /+/'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.

$ perl -wle 'print split qr/+/'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.
 
T

Tad McClellan

it_says_BALLS_on_your forehead said:
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?


Not that I can see.

He claims that the use of the
qr// op here can help capture an error if the STRING is not a valid
regex.


I have no idea what "capture an error" might mean...

Does this make any sense?


No, unless he can give an example where using qr// gives more
info than an m// with the same pattern.

These both make the same output for instance.

perl -e 'qr/(/'

and

perl -e 'm/(/'

Can your colleague provide a counter-example that shows qr// as
somehow "better"?
 
I

it_says_BALLS_on_your forehead

Dr.Ruud said:
it_says_BALLS_on_your forehead schreef:


I don't see any difference in using /+/ or qr/+/, both give the same
error.
My gut feeling says that split does a precompile on a /PATTERN/.


$ perl -MO=Deparse -e 'print split ".+"'
print split(/.+/, $_, 0);

$ perl -wle 'print split "+"'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.

$ perl -wle 'print split /+/'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.

$ perl -wle 'print split qr/+/'
Quantifier follows nothing in regex; marked by <-- HERE in m/+ <-- HERE
/ at -e line 1.

Great, that's informative. Thanks Doc.
 
B

Brian McCauley

it_says_BALLS_on_your forehead said:
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?

It could be argued that it aids clarity.

The built-in split() function has a magic prototype such that
split(/=/, $string) is, in effect, treated as if you'd said
split(qr/=/, $string). If you wanted to write your own split()-like
function you'd need a qr// in the call.

That said, I think most Perl programmers have gotten used to seeing
split(/=/, $string) and would find the explicit qr// less clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top