Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: What can we assume in a BEGIN block ?

by leriksen (Curate)
on Oct 04, 2004 at 11:31 UTC ( #396191=note: print w/replies, xml ) Need Help??

in reply to Re: What can we assume in a BEGIN block ?
in thread What can we assume in a BEGIN block ?

I suppose this just highlights my difficulty

I 'expect' the opposite.

$foo = 1; this has been parsed first, hasn't it ? So $foo == 1, right ?

THEN the BEGIN is parsed, and so now $foo == 2, right ?

use brain;

Replies are listed 'Best First'.
Re^3: What can we assume in a BEGIN block ?
by hv (Parson) on Oct 04, 2004 at 12:03 UTC

    The code under discussion is effectively this:

    our $initialized = 0; BEGIN { $initialized = 1; } more stuff

    The significant information is that our $variable = value has both a compile-time and a runtime effect.

    When Perl parses the first line, it sees the variable declaration - the compile-time effect - and therefore knows how to interpret other references to this variable in the rest of the lexical scope (file scope in this case). It also sees the assignment, and compiles that to be executed at runtime (the runtime effect).

    When it parses the second line it sees the BEGIN block, so as soon as it has reached the end of the block it suspends parsing to execute the block. At this point the runtime assignment $initialized = 0 has not yet happened.

    It then resumes parsing the file, and once the entire file has been compiled it then executes the top-level code. This is the point at which the variable gets set to zero.


      Thank you hv, that helps a lot.

      I suppose that begs the next point - say you have a whole bunch of modules with runtime initialisations, assignments and method/sub calls - is there anything documented as to the order these get done (the same order as the sequence of 'use' statements would be a first guess) ?

      use brain;

        It's really rather obvious when you pay attention to the documented compile-time vs run-time components of things.

        For example, require is executed at run-time, first compiling the referenced code, then running it immediately after (because it is essentially a super-eval, which compiles and runs code at runtime). BEGIN-blocks move the enclosed code from runtime to compiletime. Since "use" is a combination of BEGIN and require, it causes an external file to be compiled, then executed, during the compilation of the current file.

        Also, variables are declared at compile time, subroutines are defined at compile time, but the initialization of variables doesn't happen until runtime.

        Using basic rules like this, and paying attention to the other notes (like how //o and eval work, for example), it's always possible to work out the execution order "from first principles".

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re^3: What can we assume in a BEGIN block ?
by fergal (Chaplain) on Oct 04, 2004 at 12:56 UTC

    I think you're reading

    our $foo = 1; BEGIN {...}

    as: allocate some memory and initialise it to 1 and you expect it to happen at parse/compile time but you should think of it as just a shorthand for

    our $foo; $foo = 1; BEGIN {...}
    so the memory is allocated at compile time but the initialisation happens at runtime, which will be after all the BEGIN blocks have run.

    It might seem like a good idea to initialise $foo to 1 at the same time as allocating the space but then you run into problems like

    our $foo=sub_which_hasnt_been_compiled_yet();

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://396191]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-06-25 14:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (127 votes). Check out past polls.