Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved] trouble with Perl script using "gr/.../" operator..
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
cgmd
Veteran
Veteran


Joined: 17 Feb 2005
Posts: 1585
Location: Louisiana

PostPosted: Mon Mar 05, 2007 1:49 pm    Post subject: [Solved] trouble with Perl script using "gr/.../" Reply with quote

Hi, All...
I'm trying to learn to write simple scripts using regex. Toward that end, I'm slowly going through the O'Reilly book Mastering Regular Expressions by J. Friedl.
I seem to have encountered a problem with an example Perl script which introduces the gr/.../ operator. I put together the script (p. 76 of the book) like this:
Code:

#!/usr/bin/perl

undef $/;    # Enter "file-slurp" mode
$text=<>;    # Slurp up the first file given on the command line

$text=~ s/&/&amp;/g;       # Make the basic HTML...
$text=~ s/</&lt;/g;        # ...characters &, < and >...
$text=~ s/>/&gt;/g;        # ...HTML safe.

$text=~ s/^\s*$/<p>/mg;    # Separate paragraphs.

$HostnameRegex=gr/[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info)/i;

#   Turn email addresses into links...
$text=~ s{
   \b
   # Capture the address to $1...
   (
     \w[-.\w]*                          # username
     \@
     $HostnameRegex                     # hostname
   )
   \b
}{<a href="mailto:$1">$1</a>}gix;

#   Turn HTTP URLs into links...
$text=~ s{
   \b
   # Capture the URL to $1...
   (
     http://$HostnameRegex\b            # hostname
     (
        /[-a-z0-9_:\@&?=+,.!/~*'%\$]*   # Optional path
         (?<![.,?!])                    # not allowed to end with [.,?!]
     )?
   )
}{<a href="$1">$1</a>}gix;

print $text;  # Finally, display the HTML-ized text.


When I try to execute against a sample file, Perl generates an error, which I (as a complete novice) don't understand:
Code:

cgmd ~/bin $ cat sample_file
This is a sample file.
It has seven lines.
That's all.

user@gmail.com

http://www.google.com

cgmd ~/bin $

then:

cgmd ~/bin $ perl -w ./text2HTML.pl sample_file > html-file.html
Unquoted string "gr" may clash with future reserved word at ./text2HTML.pl line 12.
Unquoted string "com" may clash with future reserved word at ./text2HTML.pl line 12.
Unquoted string "edu" may clash with future reserved word at ./text2HTML.pl line 12.
Unquoted string "info" may clash with future reserved word at ./text2HTML.pl line 12.
Unquoted string "i" may clash with future reserved word at ./text2HTML.pl line 12.
syntax error at ./text2HTML.pl line 12, near "\."
Execution of ./text2HTML.pl aborted due to compilation errors.
cgmd ~/bin $


Would someone please explain this problem with the above text2HTML.pl script at " line 12"?

Thanks!
_________________
"Primum non nocere" ---Galen


Last edited by cgmd on Mon Mar 05, 2007 10:54 pm; edited 1 time in total
Back to top
View user's profile Send private message
limn
l33t
l33t


Joined: 13 May 2005
Posts: 997

PostPosted: Mon Mar 05, 2007 5:41 pm    Post subject: Reply with quote

Code:
$HostnameRegex=gr/[-a-z0-9]+(\.[-a-z0-9]+)*\.(com|edu|info)/i;

looks like the 'g' should be a 'q'.
Back to top
View user's profile Send private message
cgmd
Veteran
Veteran


Joined: 17 Feb 2005
Posts: 1585
Location: Louisiana

PostPosted: Mon Mar 05, 2007 10:52 pm    Post subject: Reply with quote

limn wrote:
Quote:
looks like the 'g' should be a 'q'.

That is absolutely correct.. Now it runs as it should!

I guess I should add dyslexia to my expanding list of ongoing problems... :(

Thanks, limn, for helping out!
_________________
"Primum non nocere" ---Galen
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum