Reputation: 8874
package A;
$ENV{MOJO_USERAGENT_DEBUG} =1;
use Test::Mojo;
use utf8;
....
;
1;
package B;
BEGIN{ $ENV{MOJO_USERAGENT_DEBUG =1 } };
use Test::Mojo;
use utf8;
...
;
1;
the Test::Mojo
package import the Mojo::UserAgent
module:
package Test::Mojo;
...
use Mojo::UserAgent;
...
1;
why code in package A , cannot open the debug. but package B can ?
Upvotes: 0
Views: 120
Reputation: 11691
#!/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;
That should make it pretty clear ;-).
In other words: BEGIN blocks und "use" statements are being evaluated in the same compilation step and in the order of their occurrence. BEGIN and "use" have been interchanged in the two examples, so in A the BEGIN block is executed after "use Test::Mojo". My guess is that Test::Mojo evaluates the env var at compile time. If it would evaluate it at runtime, it should work.
More details? see here.
Upvotes: 2
Reputation: 386331
Because Test::Mojo checks the variable when it's loaded, you need to set the environment before then.
Upvotes: 3