[perl #75642] PATCH: Deprecate not having space between pattern and following word

[email protected] (karl williamson)
Newsgroups perl.perl5.porters
Message-ID <[email protected]>
# New Ticket Created by  karl williamson 
# Please include the string:  [perl #75642]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=75642 >


Things like "/foo/sand bar" will now generate a deprecated warning.

I don't think there was any complaint in our discussions of new modifier 
letters about deprecating words following a pattern without a gap.  I 
think the above example further shows the problem with the current behavior.

This simple patch deprecates this usage.  It does add an extra message 
if the following word isn't legitimate to the syntax error that comes 
out next, but I don't think that's worth worrying about, and may give 
someone a further clue as to where on the line the problem is.
0001-Deprecate-no-space-between-pattern-following-word.patch (text/x-patch, 2.9 KB)
>From d85de0efd523e383933963350c023bde30520952 Mon Sep 17 00:00:00 2001
From: Karl Williamson <khw@khw-desktop.(none)>
Date: Wed, 9 Jun 2010 14:40:14 -0600
Subject: [PATCH] Deprecate no space between pattern, following word

This patch raises a deprecated warning on constructs like
    $result = $a =~ m/$foo/sand $bar;
which means
    $result = $a =~ m/$foo/s and $bar;
---
 pod/perldiag.pod |   13 +++++++++++++
 t/comp/parser.t  |   19 +++++++++++++++++--
 toke.c           |    8 +++++++-
 3 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/pod/perldiag.pod b/pod/perldiag.pod
index 4d7d6ad..4cd3b0b 100644
--- a/pod/perldiag.pod
+++ b/pod/perldiag.pod
@@ -2856,6 +2856,19 @@ your system.
 (F) The indicated command line switch needs a mandatory argument, but
 you haven't specified one.
 
+=item No space between pattern and following word is deprecated
+
+(D syntax)
+
+You had a word that isn't a regex modifier immediately following a pattern
+without an intervening space.  For example, the two constructs:
+
+ $a =~ m/$foo/sand $bar
+ $a =~ m/$foo/s and $bar
+
+both currently mean the same thing, but it is planned to disallow the first form
+in the next even numbered release of Perl.
+
 =item No such class field "%s" in variable %s of type %s
 
 (F) You tried to access a key from a hash through the indicated typed variable
diff --git a/t/comp/parser.t b/t/comp/parser.t
index 8fd9453..cdf0361 100644
--- a/t/comp/parser.t
+++ b/t/comp/parser.t
@@ -3,7 +3,7 @@
 # Checks if the parser behaves correctly in edge cases
 # (including weird syntax errors)
 
-print "1..122\n";
+print "1..125\n";
 
 sub failed {
     my ($got, $expected, $name) = @_;
@@ -353,7 +353,22 @@ eval q{
 };
 is($@, "", "multiline whitespace inside substitute expression");
 
-# Add new tests HERE:
+{
+    my $message;
+    local $SIG{__WARN__} = sub { $message = $_[0] };
+    my ($a, $foo, $bar) = 1;
+    my $result;
+    my $expr = q{$result = $a =~ m/$foo/sand $bar};
+    eval $expr;
+    is($@, "", '"m/$foo/sand $bar" is valid');
+    is($result, 1, '"m/$foo/sand $bar" returns 1');
+    like($message,
+        "No space between pattern and following word",
+        '"m/$foo/sand $bar" warns');
+}
+
+
+# Add new tests above HERE:
 
 # More awkward tests for #line. Keep these at the end, as they will screw
 # with sane line reporting for any other test failures
diff --git a/toke.c b/toke.c
index 8a4af48..0c78a35 100644
--- a/toke.c
+++ b/toke.c
@@ -11884,8 +11884,14 @@ S_scan_pat(pTHX_ char *start, I32 type)
 #ifdef PERL_MAD
     modstart = s;
 #endif
-    while (*s && strchr(valid_flags, *s))
+    while (*s && strchr(valid_flags, *s)) {
 	pm->op_pmflags = S_pmflag(pm->op_pmflags, *s++);
+    }
+
+    if (isALNUM(*s)) {
+	Perl_ck_warner_d(aTHX_ packWARN(WARN_SYNTAX),
+		"No space between pattern and following word is deprecated");
+    }
 #ifdef PERL_MAD
     if (PL_madskills && modstart != s) {
 	SV* tmptoken = newSVpvn(modstart, s - modstart);
-- 
1.5.6.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.