In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original?
I usually just copy the string to a new variable then bind it to the s///
regex that does the replacement on the new string, but I was wondering if there is a better way to do this?
$newstring = $oldstring;
$newstring =~ s/foo/bar/g;
This is the idiom I've always used to get a modified copy of a string without changing the original:
(my $newstring = $oldstring) =~ s/foo/bar/g;
In perl 5.14.0 or later, you can use the new /r
non-destructive substitution modifier:
my $newstring = $oldstring =~ s/foo/bar/gr;
NOTE:
The above solutions work without g
too. They also work with any other modifiers.
SEE ALSO:
perldoc perlrequick
: Perl regular expressions quick start
The statement:
(my $newstring = $oldstring) =~ s/foo/bar/g;
Which is equivalent to:
my $newstring = $oldstring;
$newstring =~ s/foo/bar/g;
Alternatively, as of Perl 5.13.2 you can use /r
to do a non destructive substitution:
use 5.013;
#...
my $newstring = $oldstring =~ s/foo/bar/gr;
g
in your top regex?
Under use strict
, say:
(my $new = $original) =~ s/foo/bar/;
instead.
The one-liner solution is more useful as a shibboleth than good code; good Perl coders will know it and understand it, but it's much less transparent and readable than the two-line copy-and-modify couplet you're starting with.
In other words, a good way to do this is the way you're already doing it. Unnecessary concision at the cost of readability isn't a win.
Another pre-5.14 solution: http://www.perlmonks.org/?node_id=346719 (see japhy's post)
As his approach uses map
, it also works well for arrays, but requires cascading map
to produce a temporary array (otherwise the original would be modified):
my @orig = ('this', 'this sucks', 'what is this?');
my @list = map { s/this/that/; $_ } map { $_ } @orig;
# @orig unmodified
If you write Perl with use strict;
, then you'll find that the one line syntax isn't valid, even when declared.
With:
my ($newstring = $oldstring) =~ s/foo/bar/;
You get:
Can't declare scalar assignment in "my" at script.pl line 7, near ") =~"
Execution of script.pl aborted due to compilation errors.
Instead, the syntax that you have been using, while a line longer, is the syntactically correct way to do it with use strict;
. For me, using use strict;
is just a habit now. I do it automatically. Everyone should.
#!/usr/bin/env perl -wT
use strict;
my $oldstring = "foo one foo two foo three";
my $newstring = $oldstring;
$newstring =~ s/foo/bar/g;
print "$oldstring","\n";
print "$newstring","\n";
use warnings;
instead of -w
, you gain greater control: for instance, if you want to temporarily turn off warnings in a block of code.
I hate foo and bar .. who dreamed up these non descriptive terms in programming anyway?
my $oldstring = "replace donotreplace replace donotreplace replace donotreplace";
my $newstring = $oldstring;
$newstring =~ s/replace/newword/g; # inplace replacement
print $newstring;
%: newword donotreplace newword donotreplace newword donotreplace
=~ s
.)
newword donotnewword newword donotnewword newword donotnewword
foo
and bar
, his answer would have been accurate. Proving, once again, customs exist for a reason and lessons are only learned the hard way. ;)
if I just use this in oneliner, how about, sprintf("%s", $oldstring)
Success story sharing
my $new = $_ for $old =~ s/foo/bar;
would work?s/foo/bar/ for my $newstring = $oldstring;
It works, but it's far weirder.