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




Dr.Ruud wrote:
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.

Great, that's informative. Thanks Doc.

.



Relevant Pages

  • Re: When would you use qr// on a literal string?
    ... regex patterns, but a colleague uses it like so: ... qr// op here can help capture an error if the STRING is not a valid ... perl -e 'qr/(/' ... Can your colleague provide a counter-example that shows qr// as ...
    (comp.lang.perl.misc)
  • Re: When would you use qr// on a literal string?
    ... regex patterns, but a colleague uses it like so: ... It could be argued that it aids clarity. ... The built-in split() function has a magic prototype such that ...
    (comp.lang.perl.misc)
  • Re: When would you use qr// on a literal string?
    ... regex patterns, but a colleague uses it like so: ... "Gewoon is een tijger." ...
    (comp.lang.perl.misc)
  • When would you use qr// on a literal string?
    ... I thought that qr was mainly used for pre-compiling variables into ... regex patterns, but a colleague uses it like so: ... qr// op here can help capture an error if the STRING is not a valid ...
    (comp.lang.perl.misc)