Normally if you wish to change a variable with regex you do this:
$string =~ s/matchCase/changeCase/;
But is there a way to simply do the replace inline without setting it back to the variable?
I wish to use it in something like this:
my $name="jason";
print "Your name without spaces is: " $name => (/\s+/''/g);
Something like that, kind of like the preg_replace function in PHP.
Revised for Perl 5.14.
Since 5.14, with the
/rflag to return the substitution, you can do this:You can use
mapand a lexical variable.Now, you have to use a lexical because $_ will alias and thus modify your variable.
The output is
Admittedly
dowill work just as well (and perhaps better)But the lexical copying is still there. The assignment within in the
myis an abbreviation that some people prefer (not me).For this idiom, I have developed an operator I call
filter:And you call it like so: