File: 67-uplevel-scope.t

package info (click to toggle)
libscope-upper-perl 0.18-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 460 kB
  • sloc: perl: 5,592; makefile: 7
file content (73 lines) | stat: -rw-r--r-- 1,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!perl -T

use strict;
use warnings;

use Test::More tests => 4 + 3 * 2;

use Scope::Upper qw<uplevel HERE UP>;

{
 our $x = 1;
 sub {
  local $x = 2;
  sub {
   local $x = 3;
   uplevel { is $x, 3, 'global variables scoping 1' } HERE;
  }->();
 }->();
}

{
 our $x = 4;
 sub {
  local $x = 5;
  sub {
   local $x = 6;
   uplevel { is $x, 6, 'global variables scoping 2' } UP;
  }->();
 }->();
}

sub {
 "abc" =~ /(.)/;
 sub {
  "xyz" =~ /(.)/;
  uplevel { is $1, 'x', 'match variables scoping 1' } HERE;
 }->();
}->();

sub {
 "abc" =~ /(.)/;
 sub {
  "xyz" =~ /(.)/;
  uplevel { is $1, 'x', 'match variables scoping 2' } UP;
 }->();
}->();

SKIP: {
 skip 'No state variables before perl 5.10' => 3 * 2 unless "$]" >= 5.010;

 my $desc = 'state variables';

 {
  local $@;
  eval 'use feature "state"; sub herp { state $id = 123; return ++$id }';
  die $@ if $@;
 }

 sub derp {
  sub {
   &uplevel(\&herp => UP);
  }->();
 }

 for my $run (1 .. 3) {
  local $@;
  my $ret = eval {
   derp()
  };
  is $@,   '',         "$desc: run $run did not croak";
  is $ret, 123 + $run, "$desc: run $run returned the correct value";
 }
}