PERL/C2/Blocks-in-Perl/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title Of Script: BLOCKS in PERL

Author: Amol Brahmankar

Keywords: BLOCKS in perl video tutorial.


Visual Cue
Narration
Slide Welcome to the spoken tutorial on BLOCKS in Perl.
Slide: Learning Objectives In this tutorial, we will learn about the various BLOCKS available in Perl
Slide: System Requirements I am using Ubuntu Linux 12.04 operating system and Perl 5.14.2


I will also be using the gedit Text Editor.

You can use any text editor of your choice.

Slide: Prerequisites As a pre-requisite, you should have basic knowledge of Variables, Comments in Perl


Knowledge of data structures in PERL will be an added advantage.


Please go through the relevant spoken tutorials on the spoken tutorial website.

Slide Perl provides 5 special blocks.


These blocks get executed at various stages of a Perl program.


These blocks are:

  1. BEGIN
  2. END
  3. UNITCHECK
  4. CHECK
  5. INIT


Let us start with understanding the BEGIN block.
Slide
  • BEGIN block get executed at the time of compilation.
  • So, any code written inside this block gets executed first during compilation.
  • We can have several BEGIN blocks inside a Perl script.
  • These blocks will get executed in the order of declaration.
  • That is in the First define First execute pattern
Slide


BEGIN {

# Piece of code to be executed at the time of compilation

}

The syntax for BEGIN block is as follows


BEGIN in capital letters space open curly bracket

Press Enter.


Piece of code to be executed at the time of compilation

Press Enter.


Close curly bracket

Now, let us look at an example of BEGIN blocks.
Switch to the Terminal and type


gedit beginBlock.pl &and press Enter.

Open the Terminal and type


gedit beginBlock dot pl space ampersand


and press Enter.

Gedit


#!/usr/bin/perl

print "First Line in PERL script\n";

BEGIN {

print "Inside First BEGIN block\n";

}

BEGIN {

print "Inside Second BEGIN block\n";

}

BEGIN {

print "Inside Third BEGIN block\n";

}

print "Last line in PERL script\n";

This will open the beginBlock dot pl file in gedit.


Type the following piece of code as displayed on the screen.




Gedit Let us look at what I have written inside the script.


Here, we have printed some text before and after the BEGIN blocks.

In Gedittext editor Similarly, I have written one print statement in each BEGIN block.
In Gedittext editor Please note, I have not given the semicolon after the BEGIN blocks.

Putting a semicolon, will result in a syntax error, on execution of the program.

Press Ctrl S Now, press Ctrl+s to save the file.
Switch to terminal


perl beginBlock.pl

Then switch to the terminal and execute the script by typing,


perl beginBlock dot pl


and press Enter.

Highlight the output on the terminal


Inside First BEGIN block

Inside Second BEGIN block

Inside Third BEGIN block

First Line in PERL script

Last line in PERL script

You will get the output as displayed on the terminal




Terminal Notice that
  • The line written inside the first BEGIN block gets printed first and
  • The first print statement in the script actually gets printed after the BEGIN block statements.
  • BEGIN blocks gets executed in the order of their declaration.
Slide From this example, it is evident that:
  • The code written inside the BEGIN blocks gets executed first.
  • This is irrespective of the location of the BEGIN block inside PERL script.
  • BEGIN blocks always get executed in the First In First Out manner.

So one of the use of this block is to include files inside a Perl script, before actual execution starts.

Now, let us understand the END block
Slide
  • END block get executed at the end of the PERL program
  • Code written inside this block gets executed after PERL has finished executing the program.
  • We can have several END blocks inside a Perl script.
  • These blocks will get executed in reverse order of declaration
  • That is, in Last define First execute pattern.
Slide


END {

# Piece of code to be executed at the end of the PERL script

}

The syntax for END block is as follows


END in capital letters space open curly bracket

Press Enter


Piece of code to be executed at the end of the PERL script


Press Enter

Close curly bracket

Now let us look at an example of END blocks.
Switch to the Terminal and type


gedit endBlock.pl &and press Enter.

Open the Terminal and type


gedit endBlock dot pl space ampersand


and press Enter

Gedit


#!/usr/bin/perl

print "First Line in PERL script\n";

END {

print "Inside First END block\n";

}

END {

print "Inside Second END block\n";

}

END {

print "Inside Third END block\n";

}

print "Last line in PERL script\n";

This will open the endBlock dot pl file in gedit.


Type the following piece of code as displayed on the screen.




Gedit Let us look at what I have written inside this script.


  • Here we have printed some text before and after the END blocks.
  • Similarly, we have written one print statement in each END block.
  • Please note, I have not given the semicolon after the END block.
  • If we give the semicolon, there will be a syntax error on compilation.
Press Ctrl S Now, press Ctrl+s to save the file.
Switch to terminal


perl endBlock.pl


Then switch to the terminal and execute the script by typing,


perl endBlock dot pl


and press Enter.

Highlight the output on the terminal


First Line in PERL script

Last line in PERL script

Inside Third END block

Inside Second END block

Inside First END block

You will get the output as displayed on the terminal.




Terminal Notice that
  • The line written inside the END block is printed last.
  • The last print statement in the script actually gets printed before the END block statements and
  • END blocks gets executed in the reverse order of their declaration.
Slide From the example, it is evident that
  • The code written inside the END blocks get executed at the end.
  • This is irrespective of the location of the END block inside the PERL script and
  • END blocks gets executed in the Last In First Out manner.

So, one use of END block is to destroy objects created in the program, before exiting.

Slide Similarly, PERL has UNITCHECK, CHECK and INIT blocks.


These blocks are used rarely by developers and are a bit difficult to understand.


So, I will be just briefing you about these blocks.

Slide UNITCHECK, CHECK and INIT blocks are useful-
  • to catch the transition between compilation and execution phase of the main program and
  • to perform some checks or initialisation, after compilation and before execution
Slide UNITCHECK and CHECK blocks runs in Last in First out manner


whereas


INIT block runs in First In First Out manner.

Slide


UNITCHECK {

# Piece of code to be executed

}

The syntax for UNITCHECK block is as follows


UNITCHECK in capital letters space open curly bracket

Press Enter


Piece of code to be executed

Press Enter


Close curly bracket

Slide


CHECK {

# Piece of code to be executed

}

The syntax for CHECK block is as follows


CHECK in capital letters space open curly bracket

Press Enter


Piece of code to be executed

Press Enter


Close curly bracket

Slide


INIT {

# Piece of code to be initialised

}

The syntax for INIT block is as follows


INIT in capital letters space open curly bracket

Press Enter


Piece of code to be initialised

Press Enter


Close curly bracket

For better understanding, I recommend that you experiment with these blocks in your Perl scripts.
Slide: Summary Let us summarize.

In this tutorial, we have learnt -

  • BEGIN and END blocks in detail and
  • Introduction to UNITCHECK, CHECK and INIT blocks
  • using sample programs
Slide: Assignment Here is assignment for you -


Type the below code inside a PERL script;


#!/usr/bin/perl

END {

@array = ();

print "Length of an array inside END block: ", $#array + 1, "\n";

}


print "Length of an array: ", $#array + 1, "\n";


BEGIN {

@array = (1, 2, 3);

}


Execute the script and observe the output.

About the Project Watch the video available at the following link.


It summaries the Spoken Tutorial project.


If you do not have good bandwidth, you can download and watch it.

Spoken Tutorial Workshops The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates to those who pass an online test

For more details, please write to contact at spoken hyphen tutorial dot org

Acknowledgment


http://spoken-tutorial.org\NMEICT-Intro

Spoken Tutorial Project is a part of the Talk to a Teacher project


It is supported by the National Mission on Education through ICT, MHRD, Government of India.


More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro

Hope you enjoyed this Perl tutorial.

This is Amol Brahmankar signing off.


Thanks for joining

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey, PoojaMoolya