summaryrefslogtreecommitdiff
path: root/pod/perlfunc.pod
diff options
context:
space:
mode:
authorGabor Szabo <szabgab@gmail.com>2006-07-09 17:44:47 +0200
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>2006-07-10 12:01:20 +0000
commite1de3ec0f145e9a5847c51ea010d180b72d30ce9 (patch)
treee69eaf46666084abe1361e6fd0b14fa11b5aabdb /pod/perlfunc.pod
parenta6cc41194dbe50598d9f33497d88c8589cd7a8c0 (diff)
downloadperl-e1de3ec0f145e9a5847c51ea010d180b72d30ce9.tar.gz
examples in the core documentation
From: "Gabor Szabo" <szabgab@gmail.com> Message-ID: <d8a74af10607090644o5c1ee3b2p98f6aa9301898b44@mail.gmail.com> p4raw-id: //depot/perl@28526
Diffstat (limited to 'pod/perlfunc.pod')
-rw-r--r--pod/perlfunc.pod12
1 files changed, 11 insertions, 1 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 07f31c9f10..d7765507a0 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -6071,6 +6071,13 @@ that far from the end of the string. If LENGTH is omitted, returns
everything to the end of the string. If LENGTH is negative, leaves that
many characters off the end of the string.
+ my $s = "The black cat climbed the green tree";
+ my $color = substr $s, 4, 5; # black
+ my $middle = substr $s, 4, -11; # black cat climbed the
+ my $end = substr $s, 14; # climbed the green tree
+ my $tail = substr $s, -4; # tree
+ my $z = substr $s, -4, 2; # tr
+
You can use the substr() function as an lvalue, in which case EXPR
must itself be an lvalue. If you assign something shorter than LENGTH,
the string will shrink, and if you assign something longer than LENGTH,
@@ -6095,6 +6102,10 @@ replacement string as the 4th argument. This allows you to replace
parts of the EXPR and return what was there before in one operation,
just as you can with splice().
+ my $s = "The black cat climbed the green tree";
+ my $z = substr $s, 14, 7, "jumped from"; # climbed
+ # $s is now "The black cat jumped from the green tree"
+
Note that the lvalue returned by the 3-arg version of substr() acts as
a 'magic bullet'; each time it is assigned to, it remembers which part
of the original string is being modified; for example:
@@ -6107,7 +6118,6 @@ of the original string is being modified; for example:
$_ = 'pq'; print $x,"\n"; # prints 5pq9
}
-
Prior to Perl version 5.9.1, the result of using an lvalue multiple times was
unspecified.