http://www.perlmonks.org?node_id=30190


in reply to Scope of regular expression variables

The behaviour you are seeing is caused by the fact that $1 and friends are only overwritten by a successfull match or substitution that has backreferences.

So your code says check this expressions against $1, if that matched then and only then will $1, $2 & $3 be given the new values. If it didn't match, then there is no statement to overwrite $1, so your that your else clause is checking the same value of $1.

Nuance

  • Comment on RE: Scope of regular expression variables