Created
July 24, 2024 12:32
-
-
Save jpbochi/bc66c701f05659e872f620b4fc53bb42 to your computer and use it in GitHub Desktop.
perl lifecycle test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# https://perldoc.perl.org/perlmod#BEGIN%2C-UNITCHECK%2C-CHECK%2C-INIT-and-END | |
# Try these: | |
# perl ./lifecycle.pl | |
# perl -c ./lifecycle.pl | |
package le_module; | |
print " 9.a. module print\n"; | |
BEGIN { print " 0.a. module BEGIN\n" } | |
END { print "17. module END\n" } | |
INIT { print " 6.a. module INIT\n" } | |
sub import { | |
print " 0.b. module import\n"; | |
} | |
package lifecycle; | |
BEGIN { 'le_module'->import; } # nearly equivalent of `use le_module;` | |
# begincheck | |
print "10. Ordinary code runs at runtime.\n"; | |
END { print "16. So this is the end of the tale.\n" } | |
INIT { print " 7. INIT blocks run FIFO just before runtime.\n" } | |
UNITCHECK { | |
print " 4. And therefore before any CHECK blocks.\n" | |
} | |
CHECK { print " 6. So this is the sixth line.\n" } | |
print "11. It runs in order, of course.\n"; | |
BEGIN { print " 1. BEGIN blocks run FIFO during compilation.\n" } | |
END { print "15. Read perlmod for the rest of the story.\n" } | |
CHECK { print " 5. CHECK blocks run LIFO after all compilation.\n" } | |
INIT { print " 8. Run this again, using Perl's -c switch.\n" } | |
print "12. This is anti-obfuscated code.\n"; | |
END { print "14. END blocks run LIFO at quitting time.\n" } | |
BEGIN { print " 2. So this line comes out second.\n" } | |
UNITCHECK { | |
print " 3. UNITCHECK blocks run LIFO after each file is compiled.\n" | |
} | |
INIT { print " 9. You'll see the difference right away.\n" } | |
print "13. It only _looks_ like it should be confusing.\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment