What is the cleanest way of picking up a match variable from a substitution 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 the cleanest way of doing it?
As others have pointed out, TIMTOWTDI.
I’d personnally wrap it up as a single expression, so as not to distract too much from the point: