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) HerongYang.com. All Rights Reserved.
#
   $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...

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

Name Spaces and Perl Module Files

 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

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB