in reply to Conditionals within Regex

print "match\n" if (/(\d{1,3})%/, $1>91);
That code has a subtle bug in it. If the regex doesn't match, $1 will still be set to its previous value...
#!/usr/bin/perl -w use strict; "abc 123" =~ /(\d+)/; $_ = "its a tub of lard"; print "'$_' matches\n" if (/(\d{1,3})%/, $1>91); __END__ 'its a tub of lard' matches
Replacing the comma with && will fix it.
if (/(\d{1,3})%/ && $1>91);
In general, don't use $1 and friends without first checking that the regex you think they belong to actually matched.

-Blake

Replies are listed 'Best First'.
Re: Re: Conditionals within Regex
by jlf (Scribe) on Sep 16, 2002 at 04:24 UTC
    Thank you for pointing this out. You may have prevented several hands full of hair from being torn out at a future date :)

    Josh