/
variables-and-packages.t
147 lines (119 loc) · 3.21 KB
/
variables-and-packages.t
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use v6;
use Test;
plan 37;
# L<S02/Names/"The following pseudo-package names are reserved">
ok !eval('module MY; 1'), 'MY is an out of scope name';
ok !eval('module OUR; 1'), 'OUR is an out of scope name';
ok !eval('module GLOBAL; 1'), 'GLOBAL is an out of scope name';
ok !eval('module PROCESS; 1'), 'PROCESS is an out of scope name';
ok !eval('module OUTER; 1'), 'OUTER is an out of scope name';
ok !eval('module CALLER; 1'), 'CALLER is an out of scope name';
ok !eval('module CONTEXT; 1'), 'CONTEXT is an out of scope name';
ok !eval('module SUPER; 1'), 'SUPER is an out of scope name';
ok !eval('module COMPILING; 1'), 'COMPILING is an out of scope name';
# L<S02/Names/The current lexical symbol table is now accessible>
# XXX -- dunno why test test fails, but the next outer test works. --iblech
#?rakudo skip 'OUTER::'
{ my $a = 1; {
my $a=2; {
my $a=3;
is($a, 3, 'get regular a');
is($OUTER::a, 2, 'get $OUTER::a');
is($OUTER::OUTER::a, 1, 'get $OUTER::OUTER::a');
}}}
#?rakudo skip 'OUTER::'
{
my $a = 1;
is $a, 1, 'get regular $a (1)';
{
my $a = 2;
is $a, 2, 'get new regular $a (1)';
{
my $a = 3;
is $a, 3, 'get very new regular $a';
is $OUTER::a, 2, 'get $OUTER::a';
is $OUTER::OUTER::a, 1, 'get $OUTER::OUTER::a';
}
}
}
# TODO: more smartlinks
{
my $a = 3;
my $sub = { $a++ };
{
my $a = -10;
is $a, -10, 'get regular $a';
is $sub(), 3, 'get hidden $a (1)';
is $sub(), 4, 'get hidden $a (2)';
is $sub(), 5, 'get hidden $a (3)';
}
}
{
my $sub = -> $stop {
my $x = 3;
if $stop {
$x++;
} else {
$sub(1);
$x;
}
};
is $sub(0), 3,
"recursively called subref shouldn't stomp on the lexical vars of other instances";
}
{
sub stomptest ($stop) {
my $x = 3;
if $stop {
$x++;
} else {
stomptest 1;
$x;
}
};
is stomptest(0), 3,
"recursively called sub shouldn't stomp on the lexical vars of other instances";
}
{
#?rakudo todo 'nom regression'
nok foo().defined, "get variable not yet declared using a sub (1)";
is foo(), 1, "get variable not yet declared using a sub (2)";
is foo(), 2, "get variable not yet declared using a sub (3)";
my $a;
sub foo { $a++ }
}
{
#?rakudo todo 'nom regression'
nok bar().defined, "runtime part of my not yet executed (1)";
is bar(), 1, "runtime part of my not yet executed (2)";
is bar(), 2, "runtime part of my not yet executed (3)";
my $a = 3;
sub bar { $a++ }
}
#?rakudo skip 'BEGIN'
{
is baz(), 3, "initilization from BEGIN block (1)";
is baz(), 4, "initilization from BEGIN block (2)";
is baz(), 5, "initilization from BEGIN block (3)";
my $a; BEGIN { $a = 3 };
sub baz { $a++ }
}
#?rakudo skip 'nom regression'
{
{
my $a = 3;
our sub grtz { $a++ }
}
is grtz(), 3, "get real hidden var using a sub (1)";
is grtz(), 4, "get real hidden var using a sub (1)";
is grtz(), 5, "get real hidden var using a sub (1)";
}
{
my $a;
sub rmbl { $a++ }
#?rakudo todo 'nom regression'
nok rmbl().defined, "var captured by sub is the right var (1)";
$a++;
is rmbl(), 2, "var captured by sub is the right var (2)";
}
# vim: ft=perl6