On Sun, Oct 02, 2005 at 12:34:53PM -0700, John W. Krahn wrote: > > To sum what up? What who is saying? What previous e-mail?
Crap... I apologize, the email was way too long so I stripped it entirely, being sure that the list will se Jendas reply. It was very thorough so I am posting it below: ==================== > Hello list, > I have some trouble understanding why a BEGIN block behaves the way it > does. Here are two files to demonstrate: > > ====== test ========= > #!/usr/bin/perl > use warnings; > use strict; > use test2 qw($var); > > our $var2 = 'second'; > > BEGIN { > print "I see $var but don't see $var2\n"; > } > > > ===== test2.pm ======= > package test2; > > use warnings; > use strict; > use Exporter; > our @ISA = qw (Exporter); > our @EXPORT_OK = qw ($var); > > our $var = 'first'; > > 1; > > > What makes the difference? use Module; is roughly equivalent to BEGIN { require Module; Module->import(); } This means that loading the module and executing the free code within happens as soon as the use statement is fully parsed. And therefore before the following BEGIN blocks. (By free code I mean code that's not in a BEGIN{}, END{}, INIT{} or CHECK{} block or a subroutine.) In your example the our $var2 = 'second'; is above the BEGIN{} block, so it it PARSED before the BEGIN block, but is executed only after all code in the main script has been parsed. While a BEGIN{} block is executed as soon as it's fully parsed. Maybe it will be more clear from this example: #main.pl print "This runs after all BEGIN blocks and use statements\n"; BEGIN { print "This is the first thing that runs\n"; } print "Second thing to run after all BEGIN blocks and use statements\n"; use BeginModule; print "Third thing to run after all BEGIN blocks and use statements\n"; BEGIN { print "This runs just after the use statement, but before the free code\n" } print "This runs the very last\n"; __END__ #BeginModule print "In BeginModule: This runs after the BEGIN\n"; BEGIN { print "In BeginModule: This is the first thing in the module that runs\n"; } print "In BeginModule: This is the last thing that runs in the module\n"; 1; __END__ Jenda ===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz ===== When it comes to wine, women and song, wizards are allowed to get drunk and croon as much as they like. -- Terry Pratchett in Sourcery -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>