in reply to Dealing with undefined captures in a substitution

Using || is less painful than defined($1)?...
sub munge { my $str = shift; $str =~ s/^R(?:([12])|(3))?$/(${1}||"")."text".($2||"")/e; $str; }
Other than that, I don't know of other more elegant options.

Edit:Anonymous Monk below is correct - _if_ our regex could capture a zero for $1 or $2, we'd have to do the defined()?: approach. But 0 can't match either capture here...


Mike

Replies are listed 'Best First'.
Re^2: Dealing with undefined captures in a substitution
by Anonymous Monk on Aug 04, 2007 at 09:55 UTC
    That is dangerous if you're possibly matching "0".