Skip to content

Commit 5876004

Browse files
committed
Add BEGIN, UNITCHECK, CHECK, INIT and END section
This was completely missing from the 5 -> 6 migration docs, afaics
1 parent 50d854a commit 5876004

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

doc/Language/5to6-nutshell.pod6

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,43 @@ As with Perl 5, comments work as usual in regexes.
11261126
11271127
/ word #`(match lexical "word") /
11281128
1129+
=head1 BEGIN, UNITCHECK, CHECK, INIT and END
1130+
1131+
Except for C<UNITCHECK>, all of these special blocks exist in Perl 6 as well.
1132+
In Perl 6, these are called L<Phasers|/language/phasers>. But there are some
1133+
differences!
1134+
1135+
=head2 UNITCHECK becomes CHECK
1136+
1137+
There is currently B<no> direct equivalent of C<CHECK> blocks in Perl 6.
1138+
The C<CHECK> phaser in Perl 6 has the same semantics as the C<UNITCHECK>
1139+
block in Perl 5: it gets run whenever the compilation unit in which it
1140+
occurs, has finished parsing. This is considered a much saner semantic
1141+
than the current semantics of C<CHECK> blocks in Perl 5. But for
1142+
compatibility reasons, it was impossible to change the semantics of C<CHECK>
1143+
blocks in Perl 5, so a C<UNITCHECK> block was introduced in 5.10. So it
1144+
was decided that the Perl 6 C<CHECK> phaser would follow the saner Perl 5
1145+
C<UNITCHECK> semantics.
1146+
1147+
=head2 No block necessary
1148+
1149+
In Perl 5, these special blocks B<must> have curly braces, which implies a
1150+
separate scope. In Perl 6 this is not necessary, allowing these special
1151+
blocks to share their scope with the surrounding lexical scope.
1152+
1153+
=for code :lang<perl5>
1154+
my $foo; # Perl 5
1155+
BEGIN { $foo = 42 }
1156+
=for code
1157+
BEGIN my $foo = 42; # Perl 6
1158+
1159+
=head2 Changed semantics with regards to precompilation
1160+
1161+
If you put C<BEGIN> and C<CHECK> phasers in a module that is being
1162+
precompiled, then these phasers will B<only> be executed during precompilation
1163+
and B<not> when a precompiled module is being loaded. So when porting module
1164+
code from Perl 5, you may need to change C<BEGIN> and C<CHECK> blocks to
1165+
C<INIT> blocks to ensure that they're run when loading that module.
11291166
11301167
=head1 Pragmas
11311168

0 commit comments

Comments
 (0)