This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add Perl_re_op_compile function
[perl5.git] / t / op / blocks.t
CommitLineData
3c10abe3
AG
1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
9a8aa25b 9plan tests => 6;
3c10abe3
AG
10
11my @expect = qw(
12b1
13b2
14b3
15b4
16b6
17u5
18b7
19u6
20u1
21c3
22c2
23c1
24i1
25i2
26b5
27u2
28u3
29u4
30e2
31e1
32 );
33my $expect = ":" . join(":", @expect);
34
9da1dd8f
DM
35# XXX tmp while re-evals are being doubly compiled:
36$expect =
37 ':b1:b2:b3:b4:b6:b6:u5:b7:u6:u5:u1:c3:c2:c2:c1:i1:i2:b5:u2:u3:u4:e2:e1';
38
3c10abe3
AG
39fresh_perl_is(<<'SCRIPT', $expect,{switches => [''], stdin => '', stderr => 1 },'Order of execution of special blocks');
40BEGIN {print ":b1"}
41END {print ":e1"}
42BEGIN {print ":b2"}
43{
44 BEGIN {BEGIN {print ":b3"}; print ":b4"}
45}
46CHECK {print ":c1"}
47INIT {print ":i1"}
48UNITCHECK {print ":u1"}
49eval 'BEGIN {print ":b5"}';
50eval 'UNITCHECK {print ":u2"}';
51eval 'UNITCHECK {print ":u3"; UNITCHECK {print ":u4"}}';
52"a" =~ /(?{UNITCHECK {print ":u5"};
53 CHECK {print ":c2"};
54 BEGIN {print ":b6"}})/x;
55eval {BEGIN {print ":b7"}};
56eval {UNITCHECK {print ":u6"}};
57eval {INIT {print ":i2"}};
58eval {CHECK {print ":c3"}};
59END {print ":e2"}
60SCRIPT
61
62@expect =(
63# BEGIN
64qw( main bar myfoo foo ),
65# UNITCHECK
66qw( foo myfoo bar main ),
67# CHECK
68qw( foo myfoo bar main ),
69# INIT
70qw( main bar myfoo foo ),
71# END
72qw(foo myfoo bar main ));
73
74$expect = ":" . join(":", @expect);
75fresh_perl_is(<<'SCRIPT2', $expect,{switches => [''], stdin => '', stderr => 1 },'blocks interact with packages/scopes');
76BEGIN {$f = 'main'; print ":$f"}
77UNITCHECK {print ":$f"}
78CHECK {print ":$f"}
79INIT {print ":$f"}
80END {print ":$f"}
81package bar;
82BEGIN {$f = 'bar';print ":$f"}
83UNITCHECK {print ":$f"}
84CHECK {print ":$f"}
85INIT {print ":$f"}
86END {print ":$f"}
87package foo;
88{
89 my $f;
90 BEGIN {$f = 'myfoo'; print ":$f"}
91 UNITCHECK {print ":$f"}
92 CHECK {print ":$f"}
93 INIT {print ":$f"}
94 END {print ":$f"}
95}
96BEGIN {$f = "foo";print ":$f"}
97UNITCHECK {print ":$f"}
98CHECK {print ":$f"}
99INIT {print ":$f"}
100END {print ":$f"}
101SCRIPT2
102
103@expect = qw(begin unitcheck check init end);
104$expect = ":" . join(":", @expect);
105fresh_perl_is(<<'SCRIPT3', $expect,{switches => [''], stdin => '', stderr => 1 },'can name blocks as sub FOO');
106sub BEGIN {print ":begin"}
107sub UNITCHECK {print ":unitcheck"}
108sub CHECK {print ":check"}
109sub INIT {print ":init"}
110sub END {print ":end"}
111SCRIPT3
8ed49485
FC
112
113fresh_perl_is(<<'SCRIPT70614', "still here",{switches => [''], stdin => '', stderr => 1 },'eval-UNITCHECK-eval (bug 70614)');
114eval "UNITCHECK { eval 0 }"; print "still here";
115SCRIPT70614
75bd28cf
FC
116
117# [perl #78634] Make sure block names can be used as constants.
118use constant INIT => 5;
119::is INIT, 5, 'constant named after a special block';
9a8aa25b
FC
120
121# [perl #108794] context
122fresh_perl_is(<<'SCRIPT3', <<expEct,{stderr => 1 },'context');
123sub context {
124 print qw[void scalar list][wantarray + defined wantarray], "\n"
125}
126BEGIN {context}
127UNITCHECK {context}
128CHECK {context}
129INIT {context}
130END {context}
131SCRIPT3
132void
133void
134void
135void
136void
137expEct