develooper Front page | perl.perl5.porters | Postings from December 2007

Re: Changes in m//m in 5.10

Thread Previous | Thread Next
From:
avar
Date:
December 23, 2007 05:12
Subject:
Re: Changes in m//m in 5.10
Message ID:
86zlw2usuo.fsf@cpan.org
diff -ru ../perl-current/pod/perldiag.pod ./pod/perldiag.pod
--- ../perl-current/pod/perldiag.pod	2007-10-30 18:32:36.000000000 +0000
+++ ./pod/perldiag.pod	2007-12-23 00:47:46.000000000 +0000
@@ -4649,6 +4649,32 @@
 if the array is tied to a class which implements a PUSH method. If so,
 you can write it as C<push(@tied_array,())> to avoid this warning.
 
+=item Useless use of regex modifiers in regex interpolation
+
+(W regexp) Your pattern consists of a variable containing a
+precompiled regular expression pattern whose behavior will not be
+changed by the pattern modifiers provided.
+
+Prior to version 5.10 perl had a bug that would cause /m to be applied
+globally, the canonical example of which is the following:
+
+    my $buffer = qq{blah\nfoo\nblah};
+    my $regex = qr/^foo/;
+    my $match = $buffer =~ m/$regex/m;
+    print $match ? "Yes" : "No", "\n";
+
+As of 5.10 this bug has been fixed and the above code will print 'no'
+because the caret (^) has been interpreted as a "Start of String" by
+the qr// and the /m modifier on the m// match will have no effect.
+
+You can silence this warning by adding a (?:) to the pattern like
+
+  m/$regex(?:)/m
+
+however the modifier will still have no affect and this will incur a
+run time speed penalty. Better would be to move the modifier to where
+the qr// is being defined.
+
 =item "use" not allowed in expression
 
 (F) The "use" keyword is recognized and executed at compile time, and
diff -ru ../perl-current/pp_ctl.c ./pp_ctl.c
--- ../perl-current/pp_ctl.c	2007-11-30 14:02:27.000000000 +0000
+++ ./pp_ctl.c	2007-12-22 23:35:36.000000000 +0000
@@ -124,6 +124,10 @@
 	regexp * const re = reg_temp_copy((regexp *)mg->mg_obj);
 	ReREFCNT_dec(PM_GETRE(pm));
 	PM_SETRE(pm, re);
+
+	if (pm->op_pmflags & (RXf_PMf_MULTILINE|RXf_PMf_SINGLELINE|RXf_PMf_FOLD) && ckWARN(WARN_REGEXP)) {
+	    Perl_warner(aTHX_ packWARN(WARN_REGEXP), "Useless use of regex modifiers in regex interpolation" );
+	}
     }
     else {
 	STRLEN len;
diff -ru ../perl-current/t/lib/warnings/pp_ctl ./t/lib/warnings/pp_ctl
--- ../perl-current/t/lib/warnings/pp_ctl	2007-10-05 11:48:19.000000000 +0000
+++ ./t/lib/warnings/pp_ctl	2007-12-23 00:52:46.000000000 +0000
@@ -257,3 +257,81 @@
 eval 'use vars; use 5.10.0';
 EXPECT
 v-string in use/require non-portable at (eval 1) line 2.
+########
+# pp_ctl.c
+use warnings 'regexp';
+
+my $rx = qr/^/;
+
+# m:
+/$rx/m;
+qr/$rx/m;
+s/$rx//m;
+
+# s:
+/$rx/s;
+qr/$rx/s;
+s/$rx//s;
+
+# i:
+/$rx/i;
+qr/$rx/i;
+s/$rx//i;
+
+# x:
+/$rx/x;
+qr/$rx/x;
+s/$rx//x;
+
+# p:
+/$rx/p;
+qr/$rx/p;
+s/$rx//p;
+
+# ms:
+/$rx/ms;
+qr/$rx/ms;
+s/$rx//ms;
+
+# msi:
+/$rx/msi;
+qr/$rx/msi;
+s/$rx//msi;
+
+# msix:
+/$rx/msix;
+qr/$rx/msix;
+s/$rx//msix;
+
+# msixp:
+/$rx/msixp;
+qr/$rx/msixp;
+s/$rx//msixp;
+
+# We don't warn on these in the current implementation but might in
+# the future:
+qr/(?-m:foo)/m;
+qr/(?-msix:foo)/msix;
+
+EXPECT
+Useless use of regex modifiers in regex interpolation at - line 7.
+Useless use of regex modifiers in regex interpolation at - line 8.
+Useless use of regex modifiers in regex interpolation at - line 9.
+Useless use of regex modifiers in regex interpolation at - line 12.
+Useless use of regex modifiers in regex interpolation at - line 13.
+Useless use of regex modifiers in regex interpolation at - line 14.
+Useless use of regex modifiers in regex interpolation at - line 17.
+Useless use of regex modifiers in regex interpolation at - line 18.
+Useless use of regex modifiers in regex interpolation at - line 19.
+Useless use of regex modifiers in regex interpolation at - line 32.
+Useless use of regex modifiers in regex interpolation at - line 33.
+Useless use of regex modifiers in regex interpolation at - line 34.
+Useless use of regex modifiers in regex interpolation at - line 37.
+Useless use of regex modifiers in regex interpolation at - line 38.
+Useless use of regex modifiers in regex interpolation at - line 39.
+Useless use of regex modifiers in regex interpolation at - line 42.
+Useless use of regex modifiers in regex interpolation at - line 43.
+Useless use of regex modifiers in regex interpolation at - line 44.
+Useless use of regex modifiers in regex interpolation at - line 47.
+Useless use of regex modifiers in regex interpolation at - line 48.
+Useless use of regex modifiers in regex interpolation at - line 49.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About