in reply to Regex question

Set the default value BEFORE the "if".

You will get inside the IF only if it is already matched.

Offense, like beauty, is in the eye of the beholder, and a fantasy.
By guaranteeing freedom of expression, the First Amendment also guarntees offense.

Replies are listed 'Best First'.
Re: Re: Regex question
by carric (Beadle) on Mar 19, 2004 at 22:39 UTC

    but.. I'm doing a bunch of stuff IN the if based on this match... =).. so setting the var outside of the if didn't help me.

    Thanks though

      To reiterate NetWallah's point, you won't get into the loop if the regex doesn't match, meaning that if you do get into the loop, there will be something in $1, so any code to set a default value won't be called. If you have to run what is presently in the if block regardless of rather or not the match was successful, then it probably shouldn't be an if block.

      You may wish to use something along the lines of thelenm's second snippet instead, something like this:

      $match = ($string =~ /foobar(...)/) ? $1 : "chiapet"; push (@array, $match); bunch of other stuff...

      note the lack of the if block...

      Just Another Perl Alchemist