This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
gv:gv_try_downgrade: Leave PL_stderrgv alone
[perl5.git] / t / op / kvaslice.t
CommitLineData
98dfcb3f
RZ
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9# use strict;
10
f263a88e 11plan tests => 39;
98dfcb3f
RZ
12
13# simple use cases
14{
15 my @a = 'a'..'z';
16
17 is( join(':', %a[0,1,2]), '0:a:1:b:2:c', "correct result and order");
18 is( join(':', %a[2,1,0]), '2:c:1:b:0:a', "correct result and order");
19 is( join(':', %a[1,0,2]), '1:b:0:a:2:c', "correct result and order");
20
21 ok( eq_hash( { %a[5,6] }, { 5 => 'f', 6 => 'g' } ), "correct hash" );
22
23 is( join(':', %a[()]), '', "correct result for empty slice");
24}
25
26# not existing elements
27{
28 my @a = 'a'..'d';
29 ok( eq_hash( { %a[3..4] }, { 3 => 'd', 4 => undef } ),
30 "not existing returned with undef value" );
31
32 ok( !exists $a[5], "no autovivification" );
33}
34
35# repeated keys
36{
37 my @a = 'a'..'d';
38 @a = %a[ (1) x 3 ];
39 ok eq_array( \@a, [ (1 => 'b') x 3 ]), "repetead keys end with repeated results";
40}
41
42# scalar context
43{
44 my @a = 'a'..'z';
45 is scalar %a[4,5,6], 'g', 'last element in scalar context';
46
47 {
48 my @warn;
49 local $SIG{__WARN__} = sub {push @warn, "@_"};
50 eval 'is( scalar %a[5], "f", "correct value");';
51
52 is (scalar @warn, 1);
53 like ($warn[0], qr/^Scalar value \%a\[5\] better written as \$a\[5\]/);
54 }
55}
56
57# autovivification
58{
59 my @a = 'a'..'b';
60
61 my @t = %a[1,2];
62 is( join(':', map {$_//'undef'} @t), '1:b:2:undef', "correct result");
63 ok( eq_array( \@a, ['a', 'b'] ), "correct array" );
64}
65
66# refs
67{
68 my $a = [ 'a'..'z' ];
69
70 is( join(':', %$a[2,3,4]), '2:c:3:d:4:e', "correct result and order");
71 is( join(':', %{$a}[2,3,4]), '2:c:3:d:4:e', "correct result and order");
72}
73
74# no interpolation
75{
76 my @a = 'a'..'b';
77 is( "%a[1,2]", q{%a[1,2]}, 'no interpolation within strings' );
78}
79
80# ref of a slice produces list
81{
82 my @a = 'a'..'z';
83 my @tmp = \%a[2,3,4];
84
85 my $ok = 1;
86 $ok = 0 if grep !ref, @tmp;
87 ok $ok, "all elements are refs";
88
89 is join( ':', map{ $$_ } @tmp ), '2:c:3:d:4:e';
90}
91
92# lvalue usage in foreach
93{
94 my @a = qw(0 1 2 3);
95 my @i = (1,3);
96 $_++ foreach %a[@i];
97 ok( eq_array( \@a, [0,2,2,4] ), "correct array" );
98 ok( eq_array( \@i, [1,3] ), "indexes not touched" );
99}
100
101# lvalue subs in foreach
102{
103 my @a = qw(0 1 2 3);
104 my @i = (1,3);
105 sub foo:lvalue{ %a[@i] };
106 $_++ foreach foo();
107 ok( eq_array( \@a, [0,2,2,4] ), "correct array" );
108 ok( eq_array( \@i, [1,3] ), "indexes not touched" );
109}
110
111# errors
112{
113 my @a = 'a'..'b';
114 # no local
115 {
116 local $@;
117 eval 'local %a[1,2]';
118 like $@, qr{^Can't modify index/value array slice in local at},
119 'local dies';
120 }
121 # no delete
122 {
123 local $@;
124 eval 'delete %a[1,2]';
125 like $@, qr{^delete argument is index/value array slice, use array slice},
126 'delete dies';
127 }
128 # no assign
129 {
130 local $@;
131 eval '%a[1,2] = qw(B A)';
132 like $@, qr{^Can't modify index/value array slice in list assignment},
133 'assign dies';
134 }
135 # lvalue subs in assignment
136 {
137 local $@;
138 eval 'sub bar:lvalue{ %a[1,2] }; bar() = "1"';
139 like $@, qr{^Can't modify index/value array slice in list assignment},
140 'not allowed as result of lvalue sub';
141 }
142}
143
144# warnings
145{
146 my @warn;
147 local $SIG{__WARN__} = sub {push @warn, "@_"};
148
149 my @a = 'a'..'c';
150 {
151 @warn = ();
95a31aad 152 my $v = eval '%a[0]';
98dfcb3f
RZ
153 is (scalar @warn, 1, 'warning in scalar context');
154 like $warn[0], qr{^Scalar value %a\[0\] better written as \$a\[0\]},
155 "correct warning text";
156 }
157 {
158 @warn = ();
159 my ($k,$v) = eval '%a[0]';
160 is ($k, 0);
161 is ($v, 'a');
95a31aad 162 is (scalar @warn, 0, 'no warning in list context');
98dfcb3f
RZ
163 }
164}
165
166# simple case with tied
167{
168 require Tie::Array;
169 tie my @a, 'Tie::StdArray';
170 @a = 'a'..'c';
171
172 ok( eq_array( [%a[1,2, 3]], [qw(1 b 2 c 3), undef] ),
173 "works on tied" );
174
175 ok( !exists $a[3], "no autovivification" );
176}
177
f263a88e
FC
178# keys/value/each treat argument as scalar
179{
180 my %h = 'a'..'b';
181 my @i = \%h;
182 my ($k,$v) = each %i[(0)]; # parens suppress "Scalar better written as"
183 is $k, 'a', 'key returned by each %array[ix]';
184 is $v, 'b', 'val returned by each %array[ix]';
185 %h = 1..10;
186 is join('-', sort keys %i[(0)]), '1-3-5-7-9', 'keys %array[ix]';
187 is join('-', sort values %i[(0)]), '10-2-4-6-8', 'values %array[ix]';
188}
189
190# \% prototype expects hash deref
191sub nowt_but_hash(\%) {}
192eval 'nowt_but_hash %_[0]';
193like $@, qr`^Type of arg 1 to main::nowt_but_hash must be hash \(not(?x:
194 ) index/value array slice\) at `,
195 '\% prototype';