Click here to get back home

Some questions about q{} and qr{}.

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Some questions about q{} and qr{}. Robbie Hatley 04-13-2008
Posted by Robbie Hatley on April 13, 2008, 9:07 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"?

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.)

Thanks in advance for your input!


===============================================================
IF YOU'RE PRESSED FOR TIME, FEEL FREE TO STOP READING HERE.
THE REMAINDER OF THIS POST IS THE WHOLE PROGRAM, FOR REFERENCE.
===============================================================


#!/usr/bin/perl

# linkify.perl

# Converts any text document into an HTML document with all of the contents of
# the original, but with any HTTP URLs converted to clickable hyperlinks.

# First print the standard opening lines of an HTML file.
# The title will be "Linkifyed HTML Document",
# the body text is in a "div" element,
# and the paragraphs will have 5-pixel margins on all 4 sides:

use strict;
use warnings;

# Print standard opening boilerplate crap for an HTML file:
print ("<html>\n");
print ("<head>\n");
print ("<title>Linkifyed HTML Document</title>\n");
print ("<style>p</style>\n");
print ("</head>\n");
print ("<body>\n");
print ("<div>\n");

# A valid URL must consist solely of the following 82 characters
#
# alphanumeric: [:alnum:] 62
# reserved: ;/?:@=& 7
# anchor-id: # 1
# encoding: % 1
# special: $_.+!*'(),- 11
# Total: 82
#

# Make a non-interpolated string version of a character class
# consisting of the above 82 URL-legal characters:
my $Legal = q;

# This regex says "find a string which is probably a URL minus the 'http://'
# part; save any such found string as a backreference":
my $Regex1 = qr

# This regex says "http or shttp or https or shttps, followed by '://',
# followed by a cluster of URL-legal characters; save any such found string
# as a backreference":
my $Regex2 = qr;

# Now loop through all lines of text in the original file, wrapping all URLS
# found in "a" and "p" elements, with the URL used as both the text and the
# "href" attribute of the "a" element:

while (<>)
{
# Linkify all http URLs, including the less-common "shttp" and "https" ones.

# This substitution says "tack 'http://' onto be beginning of any strins
# which are probably URLS sans 'http://':
sg;

# This substitution says "replace each found URL with an html anchor element
# with the found URL used both as the "href" atttribute and as the text,
# insert the anchor element into a paragraph element,
# and bracket the paragraph element with newlines":
s{\n<p><a href="$1">$1</a></p>\n}g;

# Print the edited line. If the line did not contain a URL, it will be
# printed unexpurgated. To redirect output to a file, use ">" on the
# command line.
print ($_);
}

# Print element-closure tags for div, body, html:
print ("</div>\n");
print ("</body>\n");
print ("</html>\n");

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant



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


Posted by John W. Krahn on April 13, 2008, 11:01 pm
Please log in for more thread options
Robbie Hatley wrote:
> 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.

That just adds a '\' character to your character class:

$ perl -le'$x = q; print qr'
(?-xism:[$_])
$ perl -le'$x = q; print qr'
(?-xism:[$_])

Which it doesn't look like you intended to include.

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

Read the "Gory details of parsing quoted constructs" section of:

perldoc perlop

> 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,

That is correct.

> even though
> I didn't use a /o flag? (No sense wasting CPU time
> recompiling, because the patterns are fixed.)

perldoc -q /o



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Robbie Hatley on April 14, 2008, 4:35 am
Please log in for more thread options

"John W. Krahn" wrote:

> Robbie Hatley wrote:
>
> > is variable interpolation always strictly "one pass"?
>
> Read the "Gory details of parsing quoted constructs" section of:
> perldoc perlop

Thanks for the tip, but that section doesn't actually say
whether Perl variable interpolation is single-pass or
multi-pass (recursive).

However, when I scrolled up from that section, I noticed
that one of the sections above that:
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators
*does* specify what I was looking for. It says:

"Perl does not expand multiple levels of interpolation."

Bingo. That's what I was wondering. That explains why "$_"
wasn't being interpolated in my program.

perl -le 'my $Cat=q/Fifi/; my $Dog=q/$Cat/; print qq/$Dog/;'

Prints "$Cat", not "Fifi" as I had expected. Now that I
understand why, I can avoid being surprised by that.

--
Cheers,
Robbie Hatley
perl -le 'print "4o6e7o4f0w5llc7m"'
perl -le 'print "0ttp//7ww.7ell.3om/~4onewolf/"'



Posted by Dr.Ruud on April 14, 2008, 6:33 pm
Please log in for more thread options
John W. Krahn schreef:
> Robbie Hatley wrote:

>> 1. I had a "\" before the "$" to prevent "$_" from being
>> interpolated.
>
> That just adds a '\' character to your character class:
>
> $ perl -le'$x = q; print qr'
> (?-xism:[$_])
> $ perl -le'$x = q; print qr'
> (?-xism:[$_])

But it won't match a '\':

$ perl -wle'$x = q; print $x; print length($x); print q =~
/$x/ ? 1 : 0'
[$_]
5
0

$ perl -wle'$x = q; print $x; print length($x); print q =~
/$x/ ? 1 : 0'
[$_]
5
0

$ perl -wle'$x = q; print $x; print length($x); print q =~
/$x/ ? 1 : 0'
[$_]
6
1

$ perl -wle'$x = q; print $x; print length($x); print chr(92) =~
/$x/ ? 1 : 0'
[$_]
5
0

$ perl -wle'$x = q; print $x; print length($x); print chr(92) =~
/$x/ ? 1 : 0'
[$_]
5
0

$ perl -wle'$x = q; print $x; print length($x); print chr(92)
=~ /$x/ ? 1 : 0'
[$_]
6
1

(was run with a perl 5.8.5)

--
Affijn, Ruud

"Gewoon is een tijger."


Similar ThreadsPosted
"our" from XS and some other questions April 21, 2006, 4:54 am
questions about RE! February 9, 2007, 4:21 pm
Where to ask mysql questions? July 29, 2005, 7:55 pm
Hash questions August 4, 2005, 7:02 am
some perl questions October 24, 2005, 8:18 am
2 basics questions: 1)'a' < 'b' 2)Run, but is it ok? January 18, 2006, 9:13 am
concurrency with DBI questions February 28, 2006, 12:18 pm
Other XS progamming questions April 14, 2006, 10:33 am
Questions about Inline::C September 28, 2006, 3:58 am
Style questions January 25, 2007, 5:48 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap