Using match variable $1 for a substitution in Perl
- by justintime
What is the cleanest way of picking up a match variable form a subsitution in Perl
I sometimes find myself writing
s/(something)// ;
my $x = $1 ;
then I realise that if the s/ / / fails $1 might be carrying over a value from a previous match. So I try
my $x = 'defaultvalue' ;
if ( s/(something)// )
{
$x = $1 ;
}
Is this be cleanest way of doing it.