#!/usr/bin/env perl
# vim:set shiftwidth=4 tabstop=4 expandtab ai smartindent fileformat=unix fileencoding=utf-8 syntax=perl:
# http://perldoc.perl.org/perlmod.html#BEGIN,-UNITCHECK,-CHECK,-INIT-and-END
package init;
{ 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 merely _looks_ like it should be confusing.\n"; }
sub import {
print "IMPORT\n";
}
1;
這應該使它很清楚;-)。
換句話說:BEGIN塊和「使用」語句在相同的編譯步驟中以及它們出現的順序進行評估。在這兩個例子中BEGIN和「use」已經互換,所以在BEGIN塊中的「使用Test :: Mojo」後執行。我的猜測是Test :: Mojo在編譯時評估env var。如果它會在運行時評估它,它應該工作。
更多詳細資料? see here。
我不確定你說的是實話,因爲B有一個語法錯誤,它很可能不會打開任何特別的東西。 – Ingo 2013-03-17 09:57:23