my $first; ... my $second = $first; $second =~ s/some/substitution/; #### my @output = map { my $temp = $_; $temp =~ s/some/subst/g; $temp } @input; #### use Regex::FunctionalSubstitute; my $first; ... my $second = $first \~ s/some/subst/g; #### my @output = map { $_ \~ s/some/subst/g; } @input; #### package Regex::FunctionalSubstitute; use Filter::Simple; use strict; use warnings; my $identifier = qr/\$\S+/; my $operator = qr/\\~/; my $temp = '$Regex::FunctionalSubstitute::temporary_variable'; =head1 TODO Test Suite This documentation More accurate 'identifier' regex =head1 Conversion $this = $that \~ s/one/two/; converts to: $this = ( $temp = $that, $temp =~ s/one/two/, $temp )[2] =cut FILTER_ONLY( code => sub { my $ph = $Filter::Simple::placeholder; s{ ($identifier) # the source of the substitution \s* # possible space between $operator # the operator '\~' \s* # more possible space between ($ph) # the regex itself } { ( $temp = $1, $temp =~ $2, $temp )[2] }gx; }, ); 1;