This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #123816] fix stat stacking
[perl5.git] / t / op / anonconst.t
1 #!./perl
2
3 BEGIN {
4     chdir 't';
5     require './test.pl';
6     @INC = "../lib";
7 }
8
9 plan 8;
10
11 {
12     my $w;
13     local $SIG{__WARN__} = sub { $w .= shift };
14     eval '+sub : const {}';
15     like $w, qr/^:const is experimental at /, 'experimental warning';
16 }
17
18 no warnings 'experimental::const_attr';
19
20 push @subs, sub :const{$_} for 1..10;
21 is join(" ", map &$_, @subs), "1 2 3 4 5 6 7 8 9 10",
22   ':const capturing global $_';
23
24 my $x = 3;
25 my $sub = sub : const { $x };
26 $x++;
27 is &$sub, 3, ':const capturing lexical';
28
29 $x = 3;
30 $sub = sub : const { $x+5 };
31 $x++;
32 is &$sub, 8, ':const capturing expression';
33
34 is &{sub () : const { 42 }}, 42, ':const with truly constant sub';
35
36 *foo = $sub;
37 {
38     use warnings 'redefine';
39     my $w;
40     local $SIG{__WARN__} = sub { $w .= shift };
41     *foo = sub (){};
42     like $w, qr/^Constant subroutine main::foo redefined at /,
43         ':const subs are constant';
44 }
45
46 eval 'sub bar : const';
47 like $@, qr/^:const is not permitted on named subroutines at /,
48     ':const on named stub';
49 eval 'sub baz : const { }';
50 like $@, qr/^:const is not permitted on named subroutines at /,
51     ':const on named sub';