<p>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?</p>

<p>I usually just copy the string to a new variable then bind it to the <code>s///</code> regex that does the replacement on the new string, but I was wondering if there is a better way to do this?</p>

<pre><code>$newstring = $oldstring;
$newstring =~ s/foo/bar/g;
</code></pre>

[{'answer_id': 22896, 'author': 'John Siracusa', 'author_id': 164, 'author_profile': 'https://Stackoverflow.com/users/164', 'pm_score': 9, 'selected': True, 'text': '<p>This is the idiom I\'ve always used to get a modified copy of a string without changing the original:</p>\n<pre><code>(my $newstring = $oldstring) =~ s/foo/bar/g;\n</code></pre>\n<p>In perl 5.14.0 or later, you can use the new <code>/r</code> <a href="https://metacpan.org/pod/release/FLORA/perl-5.14.2/pod/perl5140delta.pod#Non-destructive-substitution" rel="noreferrer">non-destructive substitution modifier</a>:</p>\n<pre><code>my $newstring = $oldstring =~ s/foo/bar/gr; \n</code></pre>\n<hr />\n<p><strong>NOTE:</strong><br />\nThe above solutions work without <code>g</code> too. They also work with any other modifiers.</p>\n<p><strong>SEE ALSO:</strong><br />\n<a href="https://perldoc.perl.org/perlrequick" rel="noreferrer"><code>perldoc perlrequick</code>: Perl regular expressions quick start</a></p>\n'}, {'answer_id': 24090, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 6, 'selected': False, 'text': '<p>The statement:</p>\n\n<pre><code>(my $newstring = $oldstring) =~ s/foo/bar/g;\n</code></pre>\n\n<p>Which is equivalent to:</p>\n\n<pre><code>my $newstring = $oldstring;\n$newstring =~ s/foo/bar/g;\n</code></pre>\n\n<p>Alternatively, as of Perl 5.13.2 you can use <code>/r</code> to do a non destructive substitution:</p>\n\n<pre><code>use 5.013;\n#...\nmy $newstring = $oldstring =~ s/foo/bar/gr;\n</code></pre>\n'}, {'answer_id': 55786, 'author': 'Tim Kennedy', 'author_id': 5699, 'author_profile': 'https://Stackoverflow.com/users/5699', 'pm_score': 0, 'selected': False, 'text': '<p>If you write Perl with <code>use strict;</code>, then you\'ll find that the one line syntax isn\'t valid, even when declared.</p>\n\n<p>With:</p>\n\n<pre><code>my ($newstring = $oldstring) =~ s/foo/bar/;\n</code></pre>\n\n<p>You get:</p>\n\n<pre><code>Can\'t declare scalar assignment in "my" at script.pl line 7, near ") =~"\nExecution of script.pl aborted due to compilation errors.\n</code></pre>\n\n<p>Instead, the syntax that you have been using, while a line longer, is the syntactically correct way to do it with <code>use strict;</code>.   For me, using <code>use strict;</code> is just a habit now.  I do it automatically.  Everyone should.</p>\n\n<pre><code>#!/usr/bin/env perl -wT\n\nuse strict;\n\nmy $oldstring = "foo one foo two foo three";\nmy $newstring = $oldstring;\n$newstring =~ s/foo/bar/g;\n\nprint "$oldstring","\\n";\nprint "$newstring","\\n";\n</code></pre>\n'}, {'answer_id': 62500, 'author': 'Sam Kington', 'author_id': 6832, 'author_profile': 'https://Stackoverflow.com/users/6832', 'pm_score': 5, 'selected': False, 'text': '<p>Under <code>use strict</code>, say:</p>\n\n<pre><code>(my $new = $original) =~ s/foo/bar/;\n</code></pre>\n\n<p>instead.</p>\n'}, {'answer_id': 94557, 'author': 'Josh Millard', 'author_id': 13600, 'author_profile': 'https://Stackoverflow.com/users/13600', 'pm_score': 4, 'selected': False, 'text': "<p>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.</p>\n\n<p>In other words, a good way to do this is the way you're <i>already</i> doing it.  Unnecessary concision at the cost of readability isn't a win.</p>\n"}, {'answer_id': 35829163, 'author': 'JoGotta', 'author_id': 3037683, 'author_profile': 'https://Stackoverflow.com/users/3037683', 'pm_score': 0, 'selected': False, 'text': '<p>I hate foo and bar .. who dreamed up these non descriptive terms in programming anyway?</p>\n\n<pre><code>my $oldstring = "replace donotreplace replace donotreplace replace donotreplace";\n\nmy $newstring = $oldstring;\n$newstring =~ s/replace/newword/g; # inplace replacement\n\nprint $newstring;\n%: newword donotreplace newword donotreplace newword donotreplace\n</code></pre>\n'}, {'answer_id': 44172761, 'author': 'textral', 'author_id': 1206102, 'author_profile': 'https://Stackoverflow.com/users/1206102', 'pm_score': 2, 'selected': False, 'text': '<p>Another pre-5.14 solution: <a href="http://www.perlmonks.org/?node_id=346719" rel="nofollow noreferrer">http://www.perlmonks.org/?node_id=346719</a> (see japhy\'s post)</p>\n\n<p>As his approach uses <code>map</code>, it also works well for arrays, but requires cascading <code>map</code> to produce a temporary array (otherwise the original would be modified):</p>\n\n<pre><code>my @orig = (\'this\', \'this sucks\', \'what is this?\');\nmy @list = map { s/this/that/; $_ } map { $_ } @orig;\n# @orig unmodified\n</code></pre>\n'}, {'answer_id': 67513243, 'author': 'ngn999', 'author_id': 461293, 'author_profile': 'https://Stackoverflow.com/users/461293', 'pm_score': 0, 'selected': False, 'text': '<p>if I just use this in oneliner, how about, <code>sprintf(&quot;%s&quot;, $oldstring)</code></p>\n'}]
['https://Stackoverflow.com/questions/22836', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/277/']