|
Posted by kens on April 13, 2008, 9:31 pm
Please log in for more thread options
> Today I was editing a URL-likifying program I wrote several
> weeks ago, and I ran across some issues with q{} and qr{}
> which are puzzling me.
>
> Here's an edited-for-brevity version of the program:
>
> my $Legal = q;
> my $Regex1 = qr
> my $Regex2 = qr;
> while (<>)
> {
> sg;
> s{\n<p><a href="$1">$1</a></p>\n}g;
> print ($_);
>
> }
>
> (As an afterthought, I also tacked the entire program on the
> end of this post, for anyone who's interested.)
>
> I have two questions:
>
> 1. I had a "\" before the "$" to prevent "$_" from being
> interpolated. But when I took the "\" out, the regexes
> still worked fine! Seems to me they should break, because
> $_ is now a variable rather than just "dollar sign followed
> by underscore". But $_ seems not to be interpolated.
> So, is variable interpolation always strictly "one pass"?
q{} is equivalent to the single-quote operator. Strings inside single
quotes do not get interpolated (as opposed to double quotes - "" or
qq{}.
>
> 2. I've read that qr{} "compiles" the regex; I'm hoping that
> means that the s/// operators in the while loop will not
> recompile $Regex1 and $Regex2 each iteration, even though
> I didn't use a /o flag? (No sense wasting CPU time
> recompiling, because the patterns are fixed.)
>
Based on the documentation (perldoc perlop), qr may invoke a
precompilation of the pattern. To me that implies that it is
implementation specific, but there are others with more expertise in
this area than me.
HTH, Ken
> lines deleted
>
> --
> Cheers,
> Robbie Hatley
> lonewolf aatt well dott com
> www dott well dott com slant user slant lonewolf slant
|