diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-03-21 13:06:20 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-03-21 13:06:20 +0000 |
commit | 58a9d1fc4b9782cdc8aaf9dd6fdfa2736076b173 (patch) | |
tree | 899a993a889b10bd02600ca1f907c3d07c505d61 | |
parent | 984200d0e3101dedd636f99bf5d5603033f7162d (diff) | |
download | perl-58a9d1fc4b9782cdc8aaf9dd6fdfa2736076b173.tar.gz |
Add a new warning "Negative repeat count"
for the cases $x x -1.
p4raw-id: //depot/perl@22543
-rw-r--r-- | pod/perlop.pod | 4 | ||||
-rw-r--r-- | pp.c | 5 | ||||
-rw-r--r-- | t/lib/warnings/pp | 9 |
3 files changed, 17 insertions, 1 deletions
diff --git a/pod/perlop.pod b/pod/perlop.pod index 86cb294fe3..4430fe7609 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -238,7 +238,9 @@ Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in -parentheses, it repeats the list. +parentheses, it repeats the list. If the right operand is zero or +negative, it returns an empty string or an empty list, depending on the +context. print '-' x 80; # print row of dashes @@ -1386,6 +1386,11 @@ PP(pp_repeat) dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN); { register IV count = POPi; + if (count < 0) { + if (ckWARN(WARN_MISC)) + Perl_warner(aTHX_ packWARN(WARN_MISC), "Negative repeat count"); + count = 0; + } if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { dMARK; I32 items = SP - MARK; diff --git a/t/lib/warnings/pp b/t/lib/warnings/pp index 5ed7aa0891..db4202739b 100644 --- a/t/lib/warnings/pp +++ b/t/lib/warnings/pp @@ -102,3 +102,12 @@ use utf8 ; $_ = "\x80 \xff" ; reverse ; EXPECT +######## +# pp.c +use warnings; +$a = "b" x -1; +$a = "b" x 0; +no warnings; +$a = "b" x -1; +EXPECT +Negative repeat count at - line 3. |