If you are reading this, well, you shouldn't, try
my real blog.
#!/usr/bin/perl
use perl5i;
END { say $string }
BEGIN { $string = "w" }
CHECK { $string .= "e" }
INIT { $string .= "n" }
CHECK { $string .= "d" }
END { $string .= "t" }
CHECK { $string .= "k" }
BEGIN { $string .= "o" }
INIT { $string .= "." }
INIT { $string .= "n" }
END { $string .= "e" }
BEGIN { $string .= "n" }
The code above prints out
"wonkden.net\n". It does this because the code in the
individual blocks run at specific times. It is a little easier to see when someone is not trying to make it hard to understand:
#!/usr/bin/perl
use perl5i;
BEGIN { say 1 }
BEGIN { say 2 }
CHECK { say 4 }
CHECK { say 3 }
INIT { say 5 }
INIT { say 6 }
say 7; #runtime
say 8; #runtime
END { say 10 }
END { say 9 }
BEGIN blocks run as soon as they are encountered (note, the
use statement is a BEGIN block in disguise). When all of the
BEGIN blocks have run the
CHECK blocks run, but they run in the opposite order in which they were seen (hence the printing of
4 before
3). Once all of the
CHECK blocks are done, the
INIT blocks start running. Like the
BEGIN blocks, they run in the order they were found. After the
INIT block, normal code runs. Then, finally, the
END blocks run (and like
CHECK they run in reverse order).
No comments:
Post a Comment