You seem to have a misconception about how =~
works. =~
is a binding operator that associates a variable with a regexp operator. It does not do any assignment.
The regexp operators all work by default with the topic variable $_
, so s/foo/bar/;
is the same as $_ =~ s/foo/bar/;
. No assignment occurs. The topic variable is transformed.
The case is analogous when operating on any other variable. $var =~ s/foo/bar/;
transforms $var
by replacing the first instance of foo
with bar
. No assignment occurs.
The best advice I can give you is to write Python in Python and Perl in Perl. Don't expect the two languages to be the same.
You could do like DVK suggests and write a subroutine that will reproduce the substitution behavior you are used to.
Or you could try some idiomatic Perl. Based on your expressed desire to apply multiple transformations in one line, I've provided a couple examples you might find useful.
Here I use a for
loop over one item to topicalize $var
and apply many hard-coded transformations:
for( $var ) {
s/foo/bar/;
s/fizz/buzz/;
s/whop/bop-a-loo-bop/;
s/parkay/butter/;
s/cow/burger/;
}
Or maybe you need to apply a variable group of transforms. I define a subroutine to loop over a list of array references that define old/new transformation pairs. This example takes advantage of Perl's list oriented argument processing to handle any number of transformations.
my $foo = transform(
'abcd' =>
[ 'a', 'b' ],
[ 'bb', 'c' ],
[ 'cc', 'd' ],
[ 'dd', 'DONE' ],
);
sub transform {
my $var = shift;
for (@_ ) {
my ($old, $new) = @$_;
$var =~ s/$old/$new/;
}
return $var;
}
Finally a bit of messing about to provide a version of transform that modifies its first argument:
my $foo = 'abcd';
transform_in_place(
$foo =>
[ 'a', 'b' ],
[ 'bb', 'c' ],
[ 'cc', 'd' ],
[ 'dd', 'DONE' ],
);
print "$foo\n";
sub transform_in_place {
for my $i (1..$#_ ) {
my ($old, $new) = @{$_[$i]};
$_[0] =~ s/$old/$new/;
}
}
For my own project I'd probably use one of the first two options depending on the needs of the particular problem.