Jeff Pang wrote:
Hello,John,

As you said,

BEGIN blocks are compiled and run first before any code in main and they have
lexical scope because of the {} brackets.

and in his case:

        #!/usr/bin/perl -w
        use strict;
        print "b in main: $b";
        a();
        #----------------------
        BEGIN{
                our ($b);       
                my ($fn) = @ARGV;
                $b = `cat $fn`; 

                sub a {
                        print "b in a: $b";
                }
        }



Then,are the statements of 'print "b in main: $b";a();' compiled before BEGIN 
block?or whether the opposition is right?Thanks.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com


All statements are compiled as they are encountered but BEGIN blocks are executed immediately after they are compiled. So 'print "b in main: $b"' is compiled before the BEGIN block, and under strict, is accepted since $b is a special variable. But it is not run; and in the BEGIN block it is assigned a value, which is printed out at runtime.

The rules are as follows:

All statements are compiled as they are encountered. This is called compile time.

BEGIN block and use statements are executed immediately after they are compiled. This is during compile time. And yes, each file loaded by 'use' is compiled and executed before the rest of the main file is compiled.

Everything else, that is not part of a subroutine, is executed. This phase is called runtime.

Subroutines are executed as they are called. Again, this is during runtime.


--

Just my 0.00000002 million dollars worth,
   --- Shawn

"For the things we have to learn before we can do them,
we learn by doing them."
  Aristotle

"The man who sets out to carry a cat by its tail learns something that
will always be useful and which will never grow dim or doubtful."
  Mark Twain

"Believe in the Divine, but paddle away from the rocks."
  Hindu Proverb

* Perl tutorials at http://perlmonks.org/?node=Tutorials
* A searchable perldoc is at http://perldoc.perl.org/


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to