blob: 949334bcb7091c53048f41f8c3d65dd2b7a765bb (
plain)
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
|
#!./perl -w
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl';
use strict;
$|=1;
run_multiple_progs('', \*DATA);
foreach my $code ('sub;', 'sub ($) ;', '{ $x = sub }', 'sub ($) && 1') {
eval $code;
like($@, qr/^Illegal declaration of anonymous subroutine at/,
"'$code' is illegal");
}
{
local $::TODO;
$::TODO = 'RT #17589 not completely resolved';
# Here's a patch. It makes "sub;" and similar report an error immediately
# from the lexer. However the solution is not complete, it doesn't
# handle the case "sub ($) : lvalue;" (marked as a TODO test), because
# it's handled by the lexer in separate tokens, hence more difficult to
# work out.
my $code = 'sub ($) : lvalue;';
eval $code;
like($@, qr/^Illegal declaration of anonymous subroutine at/,
"'$code' is illegal");
}
eval "sub #foo\n{print 1}";
is($@, '');
done_testing();
__END__
sub X {
my $n = "ok 1\n";
sub { print $n };
}
my $x = X();
undef &X;
$x->();
EXPECT
ok 1
########
sub X {
my $n = "ok 1\n";
sub {
my $dummy = $n; # eval can't close on $n without internal reference
eval 'print $n';
die $@ if $@;
};
}
my $x = X();
undef &X;
$x->();
EXPECT
ok 1
########
sub X {
my $n = "ok 1\n";
eval 'sub { print $n }';
}
my $x = X();
die $@ if $@;
undef &X;
$x->();
EXPECT
ok 1
########
sub X;
sub X {
my $n = "ok 1\n";
eval 'sub Y { my $p = shift; $p->() }';
die $@ if $@;
Y(sub { print $n });
}
X();
EXPECT
ok 1
########
print sub { return "ok 1\n" } -> ();
EXPECT
ok 1
|