Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

BEGIN(), CHECK(), INIT() and END() Functions

This section provides a tutorial example on how to 4 special subroutine used by the Perl compilation process and execution process: BEGIN(), CHECK(), INIT() and END().

In a Perl source code file, you can define 4 special subroutines, which will be executed automatically by the compilation process and the execution process. The key word "sub" is optional when defining those subroutines.

1. BEGIN() - Called during the compilation time immediately after it has been defined. After its execution, the subroutine will be removed and become undefined. Multiple BEGIN()s will be called in "first in, first out" order.

2. CHECK() - Called at the end of the compilation process. Multiple CHECK()s will be called in "last in, first out" order.

3. INIT() - Called at the beginning of the execution process. Multiple INIT()s will be called in "first in, first out" order.

4. END() - Called at the end of the execution process. Multiple END()s will be called in "last in, first out" order.

Here is a demonstration program, BeginEndTest.pl,

#- BeginEndTest.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   $f = "Fortran";
   print("Printing before the first BEGIN() code...\n");
   sub BEGIN {
      print("In BEGIN: name space = ",__PACKAGE__,"\n");
      print("In BEGIN: \$f = $f...\n");
   }   	
   sub CHECK {
      print("In CHECK: name space = ",__PACKAGE__,"\n");
      print("In CHECK: \$f = $f...\n");
   }   	
   sub INIT {
      print("In INIT: name space = ",__PACKAGE__,"\n");
      print("In INIT: \$f = $f...\n");
   }   	
   sub END {
      print("In END: name space = ",__PACKAGE__,"\n");
      print("In END: \$f = $f...\n");
   }   	
   print("Printing after the first END() code...\n");

   package MyPackage;
   print("Printing before the second BEGIN() code...\n");
   sub BEGIN {
      print("In BEGIN: name space = ",__PACKAGE__,"\n");
      print("In BEGIN: \$f = $f...\n");
   }   	
   sub CHECK {
      print("In CHECK: name space = ",__PACKAGE__,"\n");
      print("In CHECK: \$f = $f...\n");
   }   	
   sub INIT {
      print("In INIT: name space = ",__PACKAGE__,"\n");
      print("In INIT: \$f = $f...\n");
   }   	
   sub END {
      print("In END: name space = ",__PACKAGE__,"\n");
      print("In END: \$f = $f...\n");
   }   	
   print("Printing after the second END() code...\n");
   exit;

No surprises in the output. But the name space (package) name symbol __PACKAGE__ is defined during compilation process:

In BEGIN: name space = main
In BEGIN: $f = ...
In BEGIN: name space = MyPackage
In BEGIN: $f = ...
In CHECK: name space = MyPackage
In CHECK: $f = ...
In CHECK: name space = main
In CHECK: $f = ...
In INIT: name space = main
In INIT: $f = ...
In INIT: name space = MyPackage
In INIT: $f = ...
Printing before the first BEGIN() code...
Printing after the first END() code...
Printing before the second BEGIN() code...
Printing after the second END() code...
In END: name space = MyPackage
In END: $f = ...
In END: name space = main
In END: $f = Fortran...

Sections in This Chapter

Including Script Codes from Other Files

do() Function - Including Script Files

require() Function - Including Script Files

"package" Statement - Switching Name Space

BEGIN(), CHECK(), INIT() and END() Functions

Defining Your Own Perl Module

CalendarModule.pm - A Sample Perl Module

Dr. Herong Yang, updated in 2008
BEGIN(), CHECK(), INIT() and END() Functions