in reply to Re: Read In A File, Manipulate It, Spit It Back Out
in thread Read In A File, Manipulate It, Spit It Back Out

using a regex resets the $n($1,$2,etc.) variables all to undefined regardless...

As it happens, this is not always the case. It is true that if the second regex succeeds, unused numbered variables from a previous regex do become undefined -- even if the new regex contains no capturing paren's.

But in the case given in the question, the $1 will not be undefined until after the second regex is evaluated. So the $1 in the second regex will reflect the value from the first regex if it succeeded. Example:

my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints: 'a2b' $str =~ s/$1/NEW/; print "$str\n"; # prints: 'NEW c34d'

As different hazzard, (distinct from the example in the stated question) if the second regex fails, the previous values are retained (not undefined). This can cause intermittent problems that can be hard to trace. Consider:

my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints 'a2b' $str =~ /(xyz)/; print "$1\n"; # prints 'a2b' again