Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the qr/STRING/ operator in Perl decide whether or not to compile STRING?

Tags:

regex

perl

The docs for qr/STRING/ say:

This operator quotes (and possibly compiles) its STRING as a regular expression.

What worries me is the part in parentheses. I can't think of any cases where I don't want it to compile a regex out of STRING. Is this parenthetical statement just weasel words to cover some future case where compiling is not desired or is there a case today (or in an earlier version of Perl) where STRING will not be compiled?

like image 304
Chas. Owens Avatar asked Dec 02 '09 15:12

Chas. Owens


1 Answers

The "possibly compiles" part of the documentation probably refers to situations like the one shown below, where the argument to qr// is an already-compiled regex:

use re 'debug';

$re1 = qr/foo/;
$re2 = qr/$re1/;

Running that program shows only one regex being compiled.

Regardless of the intent of that passage, sly allusions to internals details does not clear documentation make. I think a doc patch would be beneficial.

like image 109
John Siracusa Avatar answered Oct 30 '22 19:10

John Siracusa