]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - t/67-uplevel-scope.t
Activate the correct pad when calling the uplevel'd code
[perl/modules/Scope-Upper.git] / t / 67-uplevel-scope.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 4 + 3 * 2;
7
8 use Scope::Upper qw<uplevel HERE UP>;
9
10 {
11  our $x = 1;
12  sub {
13   local $x = 2;
14   sub {
15    local $x = 3;
16    uplevel { is $x, 3, 'global variables scoping 1' } HERE;
17   }->();
18  }->();
19 }
20
21 {
22  our $x = 4;
23  sub {
24   local $x = 5;
25   sub {
26    local $x = 6;
27    uplevel { is $x, 6, 'global variables scoping 2' } UP;
28   }->();
29  }->();
30 }
31
32 sub {
33  "abc" =~ /(.)/;
34  sub {
35   "xyz" =~ /(.)/;
36   uplevel { is $1, 'x', 'match variables scoping 1' } HERE;
37  }->();
38 }->();
39
40 sub {
41  "abc" =~ /(.)/;
42  sub {
43   "xyz" =~ /(.)/;
44   uplevel { is $1, 'x', 'match variables scoping 2' } UP;
45  }->();
46 }->();
47
48 SKIP: {
49  skip 'No state variables before perl 5.10' => 3 * 2 unless "$]" >= 5.010;
50
51  my $desc = 'state variables';
52
53  {
54   local $@;
55   eval 'use feature "state"; sub herp { state $id = 123; return ++$id }';
56   die $@ if $@;
57  }
58
59  sub derp {
60   sub {
61    &uplevel(\&herp => UP);
62   }->();
63  }
64
65  for my $run (1 .. 3) {
66   local $@;
67   my $ret = eval {
68    derp()
69   };
70   is $@,   '',         "$desc: run $run did not croak";
71   is $ret, 123 + $run, "$desc: run $run returned the correct value";
72  }
73 }