bigmacbear has asked for the wisdom of the Perl Monks concerning the following question:
I have a program that does some simple regex checking to untaint some user input prior to storing it in a database. I extracted the offending code into a test program:
#!/usr/local/bin/perl -T use strict; use warnings; my ( $id, $port, $description ); while(<>) { chomp; # Ignore comments. next if /^#/; # Do very rudimentary syntax checking on the ID and port fields ( $id, $port, $description ) = m#^\s*(\w+-\w+)\s+(\w+/\d+/\d+)\s+(.*)$#; # Here in my real program I do some database stuff. print "ID: $id, Port: $port, Description: $description\n"; }
When I run the code above it works. When I paste the exact RE assignment into my actual program (it appears in two places in the real program) it fails, even though every statement before and since is syntactically correct (obviously I am missing something). It worked a few hours ago, then all of a sudden it stopped.
Perl just gives me:
I first noticed the problem trying to run perltidy on the same code, it gives mesyntax error at ./vipatch.cgi line 237, near "( $id, $port, $descripti +on ) = m#^\s*(\w+-\"
There is no previous '(' to match a ')' on line 237 237: ( $id, $port, $description ) = m#^\s*(\w+-\w+)\s+(\w+/\d +... ^ There is no previous '(' to match a ')' on line 237 237: ... n ) = m#^\s*(\w+-\w+)\s+(\w+/\d+/\d+)\s+(.*)$#; ^ 268: ( $id, $port, $description ) = m#^\s*(\w+-\w+)\s+(\w+/\d +... ^ found Scalar where operator expected There is no previous '(' to match a ')' on line 268 268: ( $id, $port, $description ) = m#^\s*(\w+-\w+)\s+(\w+/\d +... ^ 398: The logfile vipatch.cgi.LOG may contain useful information
Of course, the log file just says the same thing. It's exasperating to see the open parenthesis right there in front of you that perltidy and apparently the Perl interpreter itself missed outright.
Perl version is 5.8.0, platform is sun4-solaris.
Where have I gone wrong here?
Update: I tried YAPE::Regex::Explain on the regex in question and it parsed it properly, no problem.
Solution: The regex wasn't the problem and there weren't any missing semicolons; there was, however, a missing $ on an object call half a page above the regex.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex syntax frustration
by Abigail-II (Bishop) on May 26, 2004 at 22:27 UTC | |
by bigmacbear (Monk) on May 26, 2004 at 23:35 UTC | |
|
Re: Regex syntax frustration
by davido (Cardinal) on May 26, 2004 at 22:31 UTC |