[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

[PATCH pod/perlmod.pod 5.005_63] Special Blocks For Less Than Gurus



Okay, I understood BEGIN and END blocks, but STOP and INIT confused
the hell out of me, I had no idea when they ran.  I didn't even
realize that the parse and compile phases were seperate!

So, I figure other people are going to be confused about it as well.
I put together some explicit docs about the order of execution of the
various special blocks relative to each other and to the various
phases of a program's execution.  I also included a little
illustrative bit of code for a still confused reader to run to get a
demonstration of the order.

Please vet the order for sanity.  I don't have any Deep Understanding
of the internals, it was simply the order I observed and which the
docs eluded to.


PS  If we had a seperate set of tutorial docs I'd stick this in there rather
than in perlmod.

PPS  Why is STOP called STOP?


--- pod/perlmod.pod	2000/01/22 21:28:44
+++ pod/perlmod.pod	2000/01/23 01:48:31
@@ -256,6 +256,60 @@
 both C<BEGIN> and<END> blocks are run when you use the B<-c> switch
 for a compile-only syntax check, although your main code is not.
 
+To summarize the order of these blocks...
+
+=over 4
+
+=item 1. Parsing and BEGIN
+
+First, perl parses the syntax of your program.  While this is
+happening it will execute any BEGIN blocks in order.
+
+=item 2. Compile
+
+Then Perl will compile your program.
+
+=item 3. STOP
+
+After compilation is done, STOP blocks will be executed in reverse
+order.  If you have run your program with the -c switch, it will stop
+here.
+
+=item 4. INIT
+
+Just before perl begins running your program, INIT blocks are run in
+order.
+
+=item 5. Run
+
+Finally your program is run.
+
+=item 6. END
+
+The very last thing Perl does is execute your END blocks in reverse
+order.
+
+=back
+
+This can all be a little confusing.  The following program should provide
+an illustration of the order blocks are run.  Try it with and without -c.
+
+        print "Now the program runs\n";
+
+        BEGIN { print "1st BEGIN block\n" }
+        END   { print "1nd END block\n"   }
+        STOP  { print "1st STOP block\n"  }
+        INIT  { print "1st INIT block\n"  }
+
+        print "And runs...\n";
+
+        BEGIN { print "2nd BEGIN block\n" }
+        END   { print "2nd END block\n"   }
+        STOP  { print "2nd STOP block\n"  } 
+        INIT  { print "2nd INIT block\n"  }
+
+        print "And keeps running\n";
+
 =head2 Perl Classes
 
 There is no special class syntax in Perl, but a package may act


Follow-Ups from:
Gurusamy Sarathy <gsar@ActiveState.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]