This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Disable lexsubs outside of feature.pm
authorFather Chrysostomos <sprout@cpan.org>
Sun, 16 Sep 2012 05:03:35 +0000 (22:03 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 16 Sep 2012 05:45:11 +0000 (22:45 -0700)
pod/perldiag.pod
t/cmd/lexsub.t
t/lib/croak/toke
t/lib/warnings/toke
toke.c

index fd09b65..e579b11 100644 (file)
@@ -1860,6 +1860,14 @@ as a goto, or a loop control statement.
 (W exiting) You are exiting a substitution by unconventional means, such
 as a return, a goto, or a loop control statement.
 
+=item Experimental "%s" subs not enabled
+
+(F) To use lexical subs, you must first enable them:
+
+    no warnings 'experimental:lexical_subs';
+    use feature 'lexical_subs';
+    my sub foo { ... }
+
 =item Explicit blessing to '' (assuming package main)
 
 (W misc) You are blessing a reference to a zero length string.  This has
index d7601b3..4dc223f 100644 (file)
@@ -8,10 +8,24 @@ BEGIN {
     *bar::like = *like;
 }
 no warnings 'deprecated';
-plan 124;
+plan 127;
+
+# -------------------- Errors with feature disabled -------------------- #
+
+eval "#line 8 foo\nmy sub foo";
+is $@, qq 'Experimental "my" subs not enabled at foo line 8.\n',
+  'my sub unexperimental error';
+eval "#line 8 foo\nCORE::state sub foo";
+is $@, qq 'Experimental "state" subs not enabled at foo line 8.\n',
+  'state sub unexperimental error';
+eval "#line 8 foo\nour sub foo";
+is $@, qq 'Experimental "our" subs not enabled at foo line 8.\n',
+  'our sub unexperimental error';
 
 # -------------------- our -------------------- #
 
+no warnings "experimental:lexical_subs";
+use feature 'lexical_subs';
 {
   our sub foo { 42 }
   is foo, 42, 'calling our sub from same package';
@@ -84,7 +98,7 @@ sub bar::c { 43 }
 
 # -------------------- state -------------------- #
 
-use 5.01; # state
+use feature 'state'; # state
 {
   state sub foo { 44 }
   isnt \&::foo, \&foo, 'state sub is not stored in the package';
index f979ea0..01cb751 100644 (file)
@@ -10,19 +10,22 @@ EXPECT
 Can't find string terminator "foo" anywhere before EOF at (eval 1) line 1.
 ########
 # NAME Missing name in "my sub"
-my sub;
+use feature 'lexical_subs'; my sub;
 EXPECT
+The lexical_subs feature is experimental at - line 1.
 Missing name in "my sub" at - line 1.
 ########
 # NAME Missing name in "our sub"
-our sub;
+use feature 'lexical_subs'; our sub;
 EXPECT
+The lexical_subs feature is experimental at - line 1.
 Missing name in "our sub" at - line 1.
 ########
 # NAME Missing name in "state sub"
-use 5.01;
+use 5.01; use feature 'lexical_subs';
 state sub;
 EXPECT
+The lexical_subs feature is experimental at - line 1.
 Missing name in "state sub" at - line 2.
 ########
 # NAME Unterminated delimiter for here document
index a0abb1b..34003c2 100644 (file)
@@ -1133,7 +1133,7 @@ Use of :locked is deprecated at - line 4.
 Use of :locked is deprecated at - line 6.
 ########
 # toke.c
-use warnings "syntax";
+use warnings "syntax"; use feature 'lexical_subs';
 sub proto_after_array(@$);
 sub proto_after_arref(\@$);
 sub proto_after_arref2(\[@$]);
@@ -1147,7 +1147,7 @@ sub underscore_fail($_$);
 sub underscore_after_at(@_);
 our sub hour (@$);
 my sub migh (@$);
-use 5.01;
+use feature 'state';
 state sub estate (@$);
 package other;
 sub hour (@$);
@@ -1158,6 +1158,7 @@ sub proto_after_array(@$);
 sub proto_after_hash(%$);
 sub underscore_fail($_$);
 EXPECT
+The lexical_subs feature is experimental at - line 2.
 Prototype after '@' for main::proto_after_array : @$ at - line 3.
 Prototype after '%' for main::proto_after_hash : %$ at - line 7.
 Illegal character after '_' in prototype for main::underscore_fail : $_$ at - line 12.
diff --git a/toke.c b/toke.c
index dee5768..e5fc735 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -7834,7 +7834,14 @@ Perl_yylex(pTHX)
 #endif
                s = scan_word(s, PL_tokenbuf, sizeof PL_tokenbuf, TRUE, &len);
                if (len == 3 && strnEQ(PL_tokenbuf, "sub", 3))
+               {
+                   if (!FEATURE_LEXSUBS_IS_ENABLED)
+                       Perl_croak(aTHX_
+                                 "Experimental \"%s\" subs not enabled",
+                                  tmp == KEY_my    ? "my"    :
+                                  tmp == KEY_state ? "state" : "our");
                    goto really_sub;
+               }
                PL_in_my_stash = find_in_my_stash(PL_tokenbuf, len);
                if (!PL_in_my_stash) {
                    char tmpbuf[1024];