This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix handling of registered warning categories
[perl5.git] / t / op / anonconst.t
CommitLineData
56c1c96f
FC
1#!./perl
2
3BEGIN {
4 chdir 't';
5 require './test.pl';
624c42e2 6 set_up_inc("../lib");
56c1c96f
FC
7}
8
4a873d7a
FC
9plan 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
18no warnings 'experimental::const_attr';
56c1c96f
FC
19
20push @subs, sub :const{$_} for 1..10;
21is join(" ", map &$_, @subs), "1 2 3 4 5 6 7 8 9 10",
22 ':const capturing global $_';
23
24my $x = 3;
25my $sub = sub : const { $x };
26$x++;
27is &$sub, 3, ':const capturing lexical';
28
29$x = 3;
30$sub = sub : const { $x+5 };
31$x++;
32is &$sub, 8, ':const capturing expression';
33
34is &{sub () : const { 42 }}, 42, ':const with truly constant sub';
35
36*foo = $sub;
37{
4a873d7a 38 use warnings 'redefine';
56c1c96f
FC
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
46eval 'sub bar : const';
47like $@, qr/^:const is not permitted on named subroutines at /,
48 ':const on named stub';
49eval 'sub baz : const { }';
50like $@, qr/^:const is not permitted on named subroutines at /,
51 ':const on named sub';