[Israel.pm] A Matching Inputted Regexps Catch

Shlomi Fish shlomif at iglu.org.il
Thu Nov 11 03:12:29 PST 2004


Well, I wrote a framework where the user could input a regex to cause 
something to happen. Then I noticed it failing in some occasions. Extracting 
the logic out of the script, caused it to operate well, and I had no idea 
what was wrong. Then I noticed that I did a :

$string =~ s!index\.html$!!;

Beforehand. Adding it to the Perl script caused it to be wrong again.

It turns what I did was something like this:

my $regex = "";
if ($path =~ /$regex/)
{
	# Do something.
}

Now, a regex that evaluates to the empty regex tries to match the last 
successful regex match. [1] And since $regex was empty, that's what happened. 
If there wasn't a successful match beforehand, it simply always returned 
TRUE. However, if it there was. It tried to match index\.html$ or whatever.

So, you should be careful with this. What I did was do a:

if ( ($regex eq "") || ($path =~ /$regex/))

But I was also suggested:

if ($path =~ /(?:$regex)/)

Regards,

	Shlomi Fish

[1] - http://tinyurl.com/5vy9c

---------------------------------------------------------------------
Shlomi Fish      shlomif at iglu.org.il
Homepage:        http://www.shlomifish.org/

Knuth is not God! It took him two days to build the Roman Empire.



More information about the Perl mailing list