From: Simon Cozens Date: Thu, 28 Dec 2000 20:33:13 +0000 (+0000) Subject: Re: [PATCH] Warn on use of reference as array elem X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d804643f1d3e26c9c696bb1ebe8344dd5f1c8f6d;p=p5sagit%2Fp5-mst-13.2.git Re: [PATCH] Warn on use of reference as array elem Message-ID: <20001228203313.A2607@deep-dark-truthful-mirror.perlhacker.org> p4raw-id: //depot/perl@8234 --- diff --git a/pod/perldiag.pod b/pod/perldiag.pod index abbdea7..be15926 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3710,6 +3710,15 @@ old way has bad side effects. (D deprecated) This was an ill-advised attempt to emulate a poorly defined B feature. Use an explicit printf() or sprintf() instead. +=item Use of reference "%s" in array index + +(W) You tried to use a reference as an array index; this probably +isn't what you mean, because references tend to be huge numbers which +take you out of memory, and so usually indicates programmer error. + +If you really do mean it, explicitly numify your reference, like so: +C<$array[0+$ref]> + =item Use of reserved word "%s" is deprecated (D deprecated) The indicated bareword is a reserved word. Future diff --git a/pp_hot.c b/pp_hot.c index 2904d9f..a24aa1d 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -2815,12 +2815,15 @@ PP(pp_aelem) { djSP; SV** svp; - IV elem = POPi; + SV* elemsv = POPs; + IV elem = SvIV(elemsv); AV* av = (AV*)POPs; U32 lval = PL_op->op_flags & OPf_MOD; U32 defer = (PL_op->op_private & OPpLVAL_DEFER) && (elem > AvFILL(av)); SV *sv; + if (SvROK(elemsv) && ckWARN(WARN_MISC)) + Perl_warner(aTHX_ WARN_MISC, "Use of reference \"%s\" as array index", SvPV_nolen(elemsv)); if (elem > 0) elem -= PL_curcop->cop_arybase; if (SvTYPE(av) != SVt_PVAV) diff --git a/t/pragma/warn/pp_hot b/t/pragma/warn/pp_hot index 698255c..5dd0380 100644 --- a/t/pragma/warn/pp_hot +++ b/t/pragma/warn/pp_hot @@ -47,6 +47,9 @@ Possible Y2K bug: about to append an integer to '19' [pp_concat] $x = "19$yy\n"; + Use of reference "%s" as array index [pp_aelem] + $x[\1] + __END__ # pp_hot.c [pp_print] use warnings 'unopened' ; @@ -228,3 +231,17 @@ $x = "19" . $yy . "\n"; EXPECT Possible Y2K bug: about to append an integer to '19' at - line 12. Possible Y2K bug: about to append an integer to '19' at - line 13. +######## +# pp_hot.c [pp_aelem] +{ +use warnings 'misc'; +print $x[\1]; +} +{ +no warnings 'misc'; +print $x[\1]; +} + +EXPECT +OPTION regex +Use of reference ".*" as array index at - line 4.