summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Dyck <dcd@tc.fluke.com>1997-03-10 15:55:44 -0800
committerChip Salzenberg <chip@atlantic.net>1997-03-09 11:57:19 +1200
commit77f720bf92f3d0100352416caeedd57936807ff2 (patch)
treecb856259f583873ccd97225db3d1c60f896ef6a3
parent699e6cd4da8c333ef83554732e73ab6734463b5d (diff)
downloadperl-77f720bf92f3d0100352416caeedd57936807ff2.tar.gz
allow C<substr 'hello', -10>
Subject: patch substr to fetch rightmost n characters This is not a 'porting' issue, but I've cc'd the list since many of the experts are there. to extract the the first 3 (or less characters in a string one can say $ perl -le '$x="abcdefg"; print substr($x , 0, 3)' abc $ perl -le '$x="ab"; print substr($x , 0, 3)' ab but to print the last 3 characters (or less) the analogy doesn't work. $ perl -le '$x="abcdefg"; print substr($x , -3)' efg $ perl -le '$x="ab"; print substr($x , -3)' I was trying to let a string grow, but keep it shorter than some maximum length. Of course a work around is to check the length of the string, eg $ perl -le '$x="ab"; print length($x)<3?$x:substr($x , 0, 3)' ab but this doesn't seem reasonable. Can anyone think of a reason against changing substr to allow this feature. I had expected it to work this way. (as it does with the following patch) $ ./perl -wle '$x="ab"; print substr($x , -3)' ab p5p-msgid: 97Mar10.155517pst.35716-2@gateway.fluke.com
-rw-r--r--pp.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/pp.c b/pp.c
index 863478d6c9..fe3c675f97 100644
--- a/pp.c
+++ b/pp.c
@@ -1521,8 +1521,10 @@ PP(pp_substr)
pos = POPi - arybase;
sv = POPs;
tmps = SvPV(sv, curlen);
- if (pos < 0)
+ if (pos < 0) {
pos += curlen + arybase;
+ if (pos < 0 && MAXARG < 3) pos = 0;
+ }
if (pos < 0 || pos > curlen) {
if (dowarn || lvalue)
warn("substr outside of string");