This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] Warn on use of reference as array elem
authorSimon Cozens <simon@netthink.co.uk>
Thu, 28 Dec 2000 20:33:13 +0000 (20:33 +0000)
committerJarkko Hietaniemi <jhi@iki.fi>
Thu, 28 Dec 2000 21:52:42 +0000 (21:52 +0000)
Message-ID: <20001228203313.A2607@deep-dark-truthful-mirror.perlhacker.org>

p4raw-id: //depot/perl@8234

pod/perldiag.pod
pp_hot.c
t/pragma/warn/pp_hot

index abbdea7..be15926 100644 (file)
@@ -3710,6 +3710,15 @@ old way has bad side effects.
 (D deprecated) This was an ill-advised attempt to emulate a poorly
 defined B<awk> 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
index 2904d9f..a24aa1d 100644 (file)
--- 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)
index 698255c..5dd0380 100644 (file)
@@ -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.