/
lexical-subs.t
101 lines (85 loc) 路 1.88 KB
/
lexical-subs.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
use v6;
use Test;
plan 13;
{
sub f() {
my sub g(){"g"}; my sub h(){g()}; h();
};
is(f(), 'g', 'can indirectly call lexical sub');
throws-like 'g', X::Undeclared::Symbols,
'lexical sub not visible outside current scope';
}
{
sub foo($x) { $x + 1 }
sub callit(&foo) {
foo(1);
}
is(foo(1), 2, 'calls subs passed as &foo parameter');
is(callit({ $^x + 2 }), 3, "lexical subs get precedence over package subs");
}
{
sub infix:<@@> ($x, $y) { $x + $y }
sub foo2(&infix:<@@>) {
2 @@ 3;
}
is(2 @@ 3, 5);
is(foo2({ $^a * $^b }), 6);
}
{
my sub test_this { #OK not used
ok 1, "Could call ok from within a lexical sub";
return 1;
}
EVAL 'test_this()';
if ($!) {
ok 0, "Could call ok from within a lexical sub";
}
}
# RT #65498
{
sub a { 'outer' };
{
my sub a { 'inner' };
is a(), 'inner', 'inner lexical hides outer sub of same name';
}
is a(), 'outer', '... but only where it is visisble';
}
{
package TestScope {
sub f { };
}
dies-ok { TestScope::f }, 'subs without scoping modifiers are not entered in the namespace';
}
# RT #57788
{
throws-like 'sub a { }; sub a { }', X::Redeclaration;
}
# RT #109322
{
my $rt109322;
sub foo ($a, $f) {
if $f {
foo('z', 0);
}
given $a {
$rt109322 ~= $a;
$rt109322 ~= $_;
}
}
foo('x', 1);
is $rt109322, 'zzxx', 'no lexical weirdness from blocks inside re-entrant subs (1)';
$rt109322 = '';
sub bar ($a, $f) {
if $f {
bar('z', 0);
}
{
$_ = $a;
$rt109322 ~= $a;
$rt109322 ~= $_;
}
};
bar('x', 1);
is $rt109322, 'zzxx', 'no lexical weirdness from blocks inside re-entrant subs (2)';
}
# vim: ft=perl6 :