Possible Duplicate:
How do I perform a Perl substitution on a string while keeping the original?
Perl: Use s/ (replace) and return new string
please, fell free to modify the title of this question if you find a better way to describe it.
Imagine we have:
my $foo = something;
my $newfoo = $foo =~ s/.*/something else/;
I thought I will have $newfoo = something else, while I found it equal to 1. I imagine that this 1 is there to say ‘matching and replacing succeed’.
The only solution I’ve found is the sequent:
my $foo = something;
my $newfoo = $foo;
$newfoo =~ s/.*/something else/;
Is there a way to accomplish the same task, i.e. creating a new variable with the result of the regexp without modifying the first variable, without the middle step?
You can assign and perform the substitution in any version of Perl with judicious use of parentheses:
Using Perl v5.14 saves you one keystroke in this case (although there may be other benefits to v5.14)