diff options
author | David Dyck <dcd@tc.fluke.com> | 1997-03-10 15:55:44 -0800 |
---|---|---|
committer | Chip Salzenberg <chip@atlantic.net> | 1997-03-09 11:57:19 +1200 |
commit | 77f720bf92f3d0100352416caeedd57936807ff2 (patch) | |
tree | cb856259f583873ccd97225db3d1c60f896ef6a3 | |
parent | 699e6cd4da8c333ef83554732e73ab6734463b5d (diff) | |
download | perl-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.c | 4 |
1 files changed, 3 insertions, 1 deletions
@@ -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"); |