Special code blocks for Perl: BEGIN, CHECK, INIT, end, and Unitcheck

Source: Internet
Author: User



This is a 5 special block of code. The key to understanding these blocks is a few points in time:


    • (1). During program compilation
    • (2). During program execution
    • (3). program execution ended but not yet exited
Begin Block
    • The begin block is executed during program compilation, which is the period of the above step (1)
    • The BEGIN block executes even if there is a syntax error in the program
    • If more than one begin block appears, the output is based on the FIFO (first in First out), which is the order from top to bottom


During the begin process, you can do something that precedes the execution of a program, such as assigning a value to a particular variable beforehand, checking whether the file exists, checking if the operating system meets the requirements, and so on.


package Foo;
use strict;
use warnings;
BEGIN {
    print "This is the first BEGIN block\n";
}

print "The program is running\n";

BEGIN {
    print "This is the second BEGIN block\n";
}


Since the begin code block executes during compilation, the print of the normal line of the program executes during execution, so the result of the above code is:


This is the first BEGIN block
This is the second BEGIN block
The program is running


There is a syntax error in the following program, but Begin also executes:


BEGIN {
    print "This is the first BEGIN block\n";
}

print "The program is running\n";

BEGIN {
    print "This is the second BEGIN block\n";
}
my $x =;


Execution Result:


syntax error at some_program.pl line 8, near "=;"
Execution of some_program.pl aborted due to compilation errors.
This is the first BEGIN block
This is the second BEGIN block


However, the error message above is not necessarily the first output, because stdout and stderr are two separate file handles and cannot guarantee the order between them.



In fact, if you import an empty list when you import a module, it is equivalent to using the Require statement in begin:


Use File::Find ();
# Equivalent to
BEGIN {
     Require File::Find;
}
End Block


The end block is executed at the end of the program execution, but before exiting, which is the period of the above step (3).


    • They also perform when the die is triggered.
    • But the end can be ignored by the signal.
    • The order in which they are executed is LIFO (last on First out), which is output from bottom to top
    • End is often used for cleaning and aftercare operations.
END {
    print "This is the first END block\n";
}

END {
    print "This is the second END block\n";
}


Output: Note that second end is output first and then


This is the second END block
This is the first END block
INIT, CHECK, and Unitcheck blocks


The INIT, CHECK, and Unitcheck blocks take effect after the program compiles and before execution. So if the syntax is wrong, they will not be triggered.


    • The check executes immediately after the compilation, i.e. the output format is LIFO after the above step (1) has just completed
    • Init immediately after check, and before step (2), the output format is FIFO
    • Unitcheck is a feature introduced after Perl 5.9.5. Used to resolve issues where check and Init are not triggered during require or do or eval import files during execution (because the import of these statements occurs during execution, and these two blocks are made during compilation). Unitcheck is executed immediately after the imported file has just been compiled and executed.
INIT {
    print "This is the first INIT block\n";
}
CHECK {
    print "This is the first CHECK block\n";
}
INIT {
    print "This is the second INIT block\n";
}
CHECK {
    print "This is the second CHECK block\n";
}


Output Result:


This is the second CHECK block
This is the first CHECK block
This is the first INIT block
This is the second INIT block


Special code blocks for Perl: BEGIN, CHECK, INIT, end, and Unitcheck


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.