diff options
author | Father Chrysostomos <sprout@cpan.org> | 2013-11-08 06:04:20 -0800 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2013-11-08 08:15:59 -0800 |
commit | 2186f8734350df0f69b852c67f593773a77590bc (patch) | |
tree | e37ff3878dd458c122881956be04c4333d220e92 /t/op/kvhslice.t | |
parent | 6a642c21192e08a710804b462f8c97902797d5b4 (diff) | |
download | perl-2186f8734350df0f69b852c67f593773a77590bc.tar.gz |
Warn for all uses of %hash{...} in scalar cx
and reword the warning slightly.
See <20131027204944.20489.qmail@lists-nntp.develooper.com>.
To avoid getting a warning about scalar context for ‘delete %a[1,2]’,
which dies anyway, I stopped scalar context from being applied to
delete’s argument. Scalar context is not meaningful here anyway, and
the context is not really scalar.
This also means that ‘delete sort’ no longer produces a warning about
scalar context before dying, so I added a test for that.
Diffstat (limited to 't/op/kvhslice.t')
-rw-r--r-- | t/op/kvhslice.t | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/t/op/kvhslice.t b/t/op/kvhslice.t index bb0f3c16b9..8acd0ab81c 100644 --- a/t/op/kvhslice.t +++ b/t/op/kvhslice.t @@ -8,7 +8,7 @@ BEGIN { # use strict; -plan tests => 43; +plan tests => 44; # 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 @@ -149,7 +151,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"; } { @@ -193,7 +195,8 @@ plan tests => 43; { my %h = 'a'..'b'; my %i = (foo => \%h); - my ($k,$v) = each %i{foo=>}; # => suppresses "Scalar better written as" + no warnings 'syntax'; + my ($k,$v) = each %i{foo=>}; is $k, 'a', 'key returned by each %hash{key}'; is $v, 'b', 'val returned by each %hash{key}'; %h = 1..10; |