6 use Test::More tests => 4 * 8 + 10 + 1 + 1;
8 use Variable::Magic qw/wizard cast VMG_UVAR/;
11 my ($name, $where, $suffix) = @_;
12 $where = defined $where ? quotemeta $where : '\(eval \d+\)';
13 my $end = defined $suffix ? "$suffix\$" : '$';
14 qr/^\Q$name\E at $where line \d+\.$end/
18 [ 'data', sub { \(my $x) }, sub { } ],
19 [ 'get', sub { \(my $x) }, sub { my $y = ${$_[0]} } ],
20 [ 'set', sub { \(my $x) }, sub { ${$_[0]} = 1 } ],
21 [ 'len', sub { [ 1 .. 3 ] }, sub { my $res = @{$_[0]} } ],
26 for my $t (@scalar_tests) {
27 my ($name, $init, $code) = @$t;
29 my $wiz = wizard $name => sub { die 'leek' };
38 like $@, expect('leek', $0),
39 "die in $name callback (direct, \$@ unset) in eval";
50 like $@, expect('leek', $0),
51 "die in $name callback (direct, \$@ set) in eval";
61 like $@, expect('leek', $0, "\nBEGIN.*"),
62 "die in $name callback (direct, \$@ unset) in BEGIN";
73 like $@, expect('leek', $0, "\nBEGIN.*"),
74 "die in $name callback (direct, \$@ set) in BEGIN";
78 ($name eq 'data' ? () : (data => sub { $_[1] })),
79 $name => sub { $_[1]->(); () },
86 &cast($x, $wiz, sub { die 'lettuce' });
89 like $@, expect('lettuce', $0),
90 "die in $name callback (indirect, \$@ unset) in eval";
97 &cast($x, $wiz, sub { die 'carrot' });
101 like $@, expect('carrot', $0),
102 "die in $name callback (indirect, \$@ unset) in eval";
109 &cast($x, $wiz, sub { die "pumpkin" });
112 like $@, expect('pumpkin', undef, "\nBEGIN.*"),
113 "die in $name callback (indirect, \$@ unset) in BEGIN";
120 &cast($x, $wiz, sub { die "chard" });
124 like $@, expect('chard', undef, "\nBEGIN.*"),
125 "die in $name callback (indirect, \$@ set) in BEGIN";
134 $wiz = wizard data => sub { $_[1] },
135 free => sub { $_[1]->(); () };
137 cast $x, $wiz, sub { die "spinach" };
140 like $@, expect('spinach', $0), 'die in free callback';
143 $wiz = wizard free => sub { die 'zucchini' };
152 like $@, expect('zucchini', $0),
153 'die in free callback in block in eval with $@ unset';
156 $wiz = wizard free => sub { die 'eggplant' };
162 die 'not reached again';
165 like $@, expect('eggplant', $0),
166 'die in free callback in block in eval with $@ set';
169 $wiz = wizard free => sub { die 'onion' };
174 like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
177 $wiz = wizard data => sub { $_[1] },
178 len => sub { $_[1]->(); $_[2] },
179 free => sub { my $x = @{$_[0]}; () };
181 cast @a, $wiz, sub { die "pepperoni" };
184 like $@, expect('pepperoni', undef, "\nBEGIN.*"),
185 'die in free callback in len callback in BEGIN';
187 # Inspired by B::Hooks::EndOfScope
190 $wiz = wizard data => sub { $_[1] },
191 free => sub { $_[1]->(); () };
193 cast %^H, $wiz, sub { die 'cabbage' };
196 like $@, expect('cabbage'), 'die in free callback at end of scope';
200 my $vm_tse_file = 't/lib/Variable/Magic/TestScopeEnd.pm';
202 eval "use Variable::Magic::TestScopeEnd";
203 like $@, expect('turnip', $vm_tse_file, "\nBEGIN(?s:.*)"),
204 'die in BEGIN in require in eval string triggers hints hash destructor';
207 Variable::Magic::TestScopeEnd::hook {
208 pass 'in hints hash destructor 2';
213 like $@, expect('tomato', undef, "\nBEGIN.*"),
214 'die in BEGIN in eval triggers hints hash destructor';
219 my $SystemRoot = $ENV{SystemRoot};
221 $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
223 system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
226 my $has_capture_tiny = do { local $@; eval 'use Capture::Tiny 0.08 (); 1' };
232 skip 'Capture::Tiny 0.08 is not installed' => $count unless $has_capture_tiny;
234 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
235 use Variable::Magic qw/wizard cast/; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }
237 skip 'Test code didn\'t run properly' => 1 unless defined $output;
238 like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
239 'die in free callback at compile time and not in eval string';
249 skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR;
250 skip 'Capture::Tiny 0.08 is not installed' => $count unless $has_capture_tiny;
252 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
253 use Variable::Magic qw/wizard cast/; BEGIN { cast %::, wizard fetch => sub { die q[salsify] } } hlagh()
255 skip 'Test code didn\'t run properly' => $count unless defined $output;
256 my $suffix = "\nExecution(?s:.*)";
257 if ($] >= 5.011005) {
258 $suffix = "(?:\nsalsify at -e line \\d+.){12}" . $suffix;
259 } elsif ($] >= 5.011) {
260 $suffix = "(?:\nsalsify at -e line \\d+.){3}" . $suffix;
262 like $output, expect('salsify', '-e', $suffix),
263 'die in free callback at compile time and not in eval string';