This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Rename a variable
[perl5.git] / t / op / kvhslice.t
index bb0f3c1..e3309ef 100644 (file)
@@ -2,13 +2,13 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
     require './test.pl';
+    set_up_inc('../lib');
 }
 
 # use strict;
 
-plan tests => 43;
+plan tests => 39;
 
 # simple use cases
 {
@@ -41,18 +41,20 @@ plan tests => 43;
 
 # scalar context
 {
+    my @warn;
+    local $SIG{__WARN__} = sub {push @warn, "@_"};
+
     my %h = map { $_ => uc $_ } 'a'..'z';
-    is scalar %h{'c','d','e'}, 'E', 'last element in scalar context';
+    is scalar eval"%h{'c','d','e'}", 'E', 'last element in scalar context';
 
-    {
-        my @warn;
-        local $SIG{__WARN__} = sub {push @warn, "@_"};
-        eval 'is( scalar %h{i}, "I", "correct value");';
+    like ($warn[0],
+     qr/^\%h\{\.\.\.\} in scalar context better written as \$h\{\.\.\.\}/);
 
-        is (scalar @warn, 1);
-        like ($warn[0],
-              qr/^Scalar value \%h\{"i"\} better written as \$h\{"i"\}/);
-    }
+    eval 'is( scalar %h{i}, "I", "correct value");';
+
+    is (scalar @warn, 2);
+    like ($warn[1],
+          qr/^\%h\{"i"\} in scalar context better written as \$h\{"i"\}/);
 }
 
 # autovivification
@@ -115,13 +117,6 @@ plan tests => 43;
         like $@, qr{^Can't modify key/value hash slice in local at},
             'local dies';
     }
-    # no delete
-    {
-        local $@;
-        eval 'delete %h{qw(a b)}';
-        like $@, qr{^delete argument is key/value hash slice, use hash slice},
-            'delete dies';
-    }
     # no assign
     {
         local $@;
@@ -132,9 +127,13 @@ plan tests => 43;
     # lvalue subs in assignment
     {
         local $@;
-        eval 'sub bar:lvalue{ %h{qw(a b)} }; bar() = "1"';
+        eval 'sub bar:lvalue{ %h{qw(a b)} }; (bar) = "1"';
         like $@, qr{^Can't modify key/value hash slice in list assignment},
             'not allowed as result of lvalue sub';
+        eval 'sub bbar:lvalue{ %h{qw(a b)} }; bbar() = "1"';
+        like $@,
+             qr{^Can't modify key/value hash slice in scalar assignment},
+            'not allowed as result of lvalue sub';
     }
 }
 
@@ -149,7 +148,7 @@ plan tests => 43;
         my $v = eval '%h{a}';
         is (scalar @warn, 1, 'warning in scalar context');
         like $warn[0],
-             qr{^Scalar value %h{"a"} better written as \$h{"a"}},
+             qr{^%h\{"a"\} in scalar context better written as \$h\{"a"\}},
             "correct warning text";
     }
     {
@@ -160,20 +159,13 @@ plan tests => 43;
         is (scalar @warn, 0, 'no warning in list context');
     }
 
-    # deprecated syntax
     {
         my $h = \%h;
-        @warn = ();
-        ok( eq_array([eval '%$h->{a}'], ['A']), 'works, but deprecated' );
-        is (scalar @warn, 1, 'one warning');
-        like $warn[0], qr{^Using a hash as a reference is deprecated},
-            "correct warning text";
+        eval '%$h->{a}';
+        like($@, qr/Can't use a hash as a reference/, 'hash reference is error' );
 
-        @warn = ();
-        ok( eq_array([eval '%$h->{"b","c"}'], [undef]), 'works, but deprecated' );
-        is (scalar @warn, 1, 'one warning');
-        like $warn[0], qr{^Using a hash as a reference is deprecated},
-            "correct warning text";
+        eval '%$h->{"b","c"}';
+        like($@, qr/Can't use a hash as a reference/, 'hash slice reference is error' );
     }
 }
 
@@ -189,16 +181,19 @@ plan tests => 43;
     ok( !exists $h{e}, "no autovivification" );
 }
 
-# keys/value/each treat argument as scalar
+# keys/value/each refuse to compile kvhslice
 {
     my %h = 'a'..'b';
     my %i = (foo => \%h);
-    my ($k,$v) = each %i{foo=>}; # => suppresses "Scalar better written as"
-    is $k, 'a', 'key returned by each %hash{key}';
-    is $v, 'b', 'val returned by each %hash{key}';
-    %h = 1..10;
-    is join('-', sort keys %i{foo=>}), '1-3-5-7-9', 'keys %hash{key}';
-    is join('-', sort values %i{foo=>}), '10-2-4-6-8', 'values %hash{key}';
+    eval '() = keys %i{foo=>}';
+    like($@, qr/Experimental keys on scalar is now forbidden/,
+         'keys %hash{key} forbidden');
+    eval '() = values %i{foo=>}';
+    like($@, qr/Experimental values on scalar is now forbidden/,
+         'values %hash{key} forbidden');
+    eval '() = each %i{foo=>}';
+    like($@, qr/Experimental each on scalar is now forbidden/,
+         'each %hash{key} forbidden');
 }
 
 # \% prototype expects hash deref