summaryrefslogtreecommitdiff
path: root/pod/perlrequick.pod
diff options
context:
space:
mode:
authorDavid Caldwell <david@porkrind.org>2009-11-23 17:24:25 -0800
committerDavid Caldwell <david@porkrind.org>2010-05-22 02:28:22 -0700
commit4f4d7508b0c2c114e5f52420e0e87a853c5f642a (patch)
tree008d80915d83e246892b83fa06e46a3b2e21c6bf /pod/perlrequick.pod
parentdd9035cd5bdeced1187df399d27d526f3b30194b (diff)
downloadperl-4f4d7508b0c2c114e5f52420e0e87a853c5f642a.tar.gz
Add s///r (non-destructive substitution).
This changes s/// so that it doesn't act destructively on its target. Instead it returns the result of the substitution (or the original string if there was no match). In addition this patch: * Adds a new warning when s///r happens in void context. * Adds a error when you try to use s///r with !~ * Makes it so constant strings can be bound to s///r with =~ * Adds documentation. * Adds some tests. * Updates various debug code so it knows about the /r flag. * Adds some new 'r' words to B::Deparse.
Diffstat (limited to 'pod/perlrequick.pod')
-rw-r--r--pod/perlrequick.pod15
1 files changed, 15 insertions, 0 deletions
diff --git a/pod/perlrequick.pod b/pod/perlrequick.pod
index 4b5e19a0fb..ded1e6cefc 100644
--- a/pod/perlrequick.pod
+++ b/pod/perlrequick.pod
@@ -440,6 +440,21 @@ of the regex in the string:
$x = "I batted 4 for 4";
$x =~ s/4/four/g; # $x contains "I batted four for four"
+The non-destructive modifier C<s///r> causes the result of the substitution
+to be returned instead of modifying C<$_> (or whatever variable the
+substitute was bound to with C<=~>):
+
+ $x = "I like dogs.";
+ $y = $x =~ s/dogs/cats/r;
+ print "$x $y\n"; # prints "I like dogs. I like cats."
+
+ $x = "Cats are great.";
+ print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~ s/Frogs/Hedgehogs/r, "\n";
+ # prints "Hedgehogs are great."
+
+ @foo = map { s/[a-z]/X/r } qw(a b c 1 2 3);
+ # @foo is now qw(X X X 1 2 3)
+
The evaluation modifier C<s///e> wraps an C<eval{...}> around the
replacement string and the evaluated result is substituted for the
matched substring. Some examples: