This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Stop reset from skipping @ % if $ is read-only
[perl5.git] / t / op / reset.t
CommitLineData
2f3c5f77
NC
1#!./perl -w
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8use strict;
9
c165afe3 10plan tests => 37;
2f3c5f77
NC
11
12package aiieee;
13
14sub zlopp {
725a61d7 15 (shift =~ m?zlopp?) ? 1 : 0;
2f3c5f77
NC
16}
17
18sub reset_zlopp {
19 reset;
20}
21
22package CLINK;
23
24sub ZZIP {
725a61d7 25 shift =~ m?ZZIP? ? 1 : 0;
2f3c5f77
NC
26}
27
28sub reset_ZZIP {
29 reset;
30}
31
32package main;
33
34is(aiieee::zlopp(""), 0, "mismatch doesn't match");
35is(aiieee::zlopp("zlopp"), 1, "match matches first time");
36is(aiieee::zlopp(""), 0, "mismatch doesn't match");
37is(aiieee::zlopp("zlopp"), 0, "match doesn't match second time");
38aiieee::reset_zlopp();
39is(aiieee::zlopp("zlopp"), 1, "match matches after reset");
40is(aiieee::zlopp(""), 0, "mismatch doesn't match");
41
42aiieee::reset_zlopp();
43
44is(aiieee::zlopp(""), 0, "mismatch doesn't match");
45is(aiieee::zlopp("zlopp"), 1, "match matches first time");
46is(CLINK::ZZIP(""), 0, "mismatch doesn't match");
47is(CLINK::ZZIP("ZZIP"), 1, "match matches first time");
48is(CLINK::ZZIP(""), 0, "mismatch doesn't match");
49is(CLINK::ZZIP("ZZIP"), 0, "match doesn't match second time");
50is(aiieee::zlopp(""), 0, "mismatch doesn't match");
51is(aiieee::zlopp("zlopp"), 0, "match doesn't match second time");
52
53aiieee::reset_zlopp();
54is(aiieee::zlopp("zlopp"), 1, "match matches after reset");
55is(aiieee::zlopp(""), 0, "mismatch doesn't match");
56
57is(CLINK::ZZIP(""), 0, "mismatch doesn't match");
58is(CLINK::ZZIP("ZZIP"), 0, "match doesn't match third time");
59
60CLINK::reset_ZZIP();
61is(CLINK::ZZIP("ZZIP"), 1, "match matches after reset");
62is(CLINK::ZZIP(""), 0, "mismatch doesn't match");
a1f22e0c 63
ca826051
FC
64sub match_foo{
65 "foo" =~ m?foo?;
66}
67match_foo();
68reset "";
69ok !match_foo(), 'reset "" leaves patterns alone [perl #97958]';
70
71$scratch::a = "foo";
72$scratch::a2 = "bar";
73$scratch::b = "baz";
74package scratch { reset "a" }
75is join("-", $scratch::a//'u', $scratch::a2//'u', $scratch::b//'u'),
76 "u-u-baz",
77 'reset "char"';
78
79$scratch::a = "foo";
80$scratch::a2 = "bar";
81$scratch::b = "baz";
82$scratch::c = "sea";
83package scratch { reset "bc" }
84is join("-", $scratch::a//'u', $scratch::a2//'u', $scratch::b//'u',
85 $scratch::c//'u'),
86 "foo-bar-u-u",
87 'reset "chars"';
88
89$scratch::a = "foo";
90$scratch::a2 = "bar";
91$scratch::b = "baz";
92$scratch::c = "sea";
93package scratch { reset "a-b" }
94is join("-", $scratch::a//'u', $scratch::a2//'u', $scratch::b//'u',
95 $scratch::c//'u'),
96 "u-u-u-sea",
97 'reset "range"';
98
99{ no strict; ${"scratch::\0foo"} = "bar" }
100$scratch::a = "foo";
101package scratch { reset "\0a" }
102is join("-", $scratch::a//'u', do { no strict; ${"scratch::\0foo"} }//'u'),
103 "u-u",
104 'reset "\0char"';
105
d6987b29
FC
106$scratch::cow = __PACKAGE__;
107$scratch::qr = ${qr//};
108$scratch::v = v6;
109$scratch::glob = *is;
110*scratch::ro = \1;
111package scratch { reset 'cqgvr' }
112is join ("-", map $_//'u', $scratch::cow, $scratch::qr, $scratch::v,
113 $scratch::glob,$scratch::ro), 'u-u-u-u-1',
114 'cow, qr, vstring, glob, ro test';
115
df70e763
FC
116@scratch::an_array = 1..3;
117%scratch::a_hash = 1..4;
118package scratch { reset 'a' }
119is @scratch::an_array, 0, 'resetting an array';
120is %scratch::a_hash, 0, 'resetting a hash';
121
c165afe3
FC
122@scratch::an_array = 1..3;
123%scratch::an_array = 1..4;
124*scratch::an_array = \1;
125package scratch { reset 'a' }
126is @scratch::an_array, 0, 'resetting array in the same gv as a ro scalar';
127is @scratch::an_array, 0, 'resetting a hash in the same gv as a ro scalar';
128is $scratch::an_array, 1, 'reset skips ro scalars in the same gv as av/hv';
129
23083432
FC
130# This used to crash under threaded builds, because pmops were remembering
131# their stashes by name, rather than by pointer.
132fresh_perl_is( # it crashes more reliably with a smaller script
133 'package bar;
134 sub foo {
135 m??;
136 BEGIN { *baz:: = *bar::; *bar:: = *foo:: }
137 # The name "bar" no langer refers to the same package
138 }
139 undef &foo; # so freeing the op does not remove it from the stash’s list
140 $_ = "";
141 push @_, ($_) x 10000; # and its memory is scribbled over
142 reset; # so reset on the original package tries to reset an invalid op
143 print "ok\n";',
144 "ok\n", {},
145 "no crash if package is effectively renamed before op is freed");
146
e39b51c6
FC
147sub _117941 { package _117941; reset }
148delete $::{"_117941::"};
149_117941();
150pass("no crash when current package is freed");
a1f22e0c
NC
151
152undef $/;
153my $prog = <DATA>;
154
155SKIP:
156{
157 eval {require threads; 1} or
158 skip "No threads", 4;
a1f22e0c
NC
159 foreach my $eight ('/', '?') {
160 foreach my $nine ('/', '?') {
161 my $copy = $prog;
162 $copy =~ s/8/$eight/gm;
163 $copy =~ s/9/$nine/gm;
164 fresh_perl_is($copy, "pass", "",
165 "first pattern $eight$eight, second $nine$nine");
166 }
167 }
168}
169
170__DATA__
171#!perl
172use warnings;
173use strict;
174
175# Note that there are no digits in this program, other than the placeholders
176sub a {
725a61d7 177m8one8;
a1f22e0c
NC
178}
179sub b {
725a61d7 180m9two9;
a1f22e0c
NC
181}
182
183use threads;
184use threads::shared;
185
186sub wipe {
ff8b4a37 187 eval 'no warnings; sub b {}; 1' or die $@;
a1f22e0c
NC
188}
189
190sub lock_then_wipe {
191 my $l_r = shift;
192 lock $$l_r;
193 cond_wait($$l_r) until $$l_r eq "B";
194 wipe;
195 $$l_r = "C";
196 cond_signal $$l_r;
197}
198
199my $lock : shared = "A";
200my $r = \$lock;
201
202my $t;
203{
204 lock $$r;
205 $t = threads->new(\&lock_then_wipe, $r);
206 wipe;
207 $lock = "B";
208 cond_signal $lock;
209}
210
211{
212 lock $lock;
213 cond_wait($lock) until $lock eq "C";
214 reset;
215}
216
217$t->join;
218print "pass\n";