Difference between revisions of "PERL/C2/Blocks-in-Perl/English"
Nancyvarkey (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
Line 8: | Line 8: | ||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | | style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual | + | | style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual Cue'''</center> |
| style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center> | | style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center> | ||
Latest revision as of 13:02, 17 June 2014
Title Of Script: BLOCKS in PERL
Author: Amol Brahmankar
Keywords: BLOCKS in perl video tutorial.
|
|
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
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
|
Slide | Perl provides 5 special blocks.
|
Let us start with understanding the BEGIN block. | |
Slide |
|
Slide
# Piece of code to be executed at the time of compilation } |
The syntax for BEGIN block is as follows
Press Enter.
Press Enter.
|
Now, let us look at an example of BEGIN blocks. | |
Switch to the Terminal and type
|
Open the Terminal and type
|
Gedit
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.
|
Gedit | Let us look at what I have written inside the script.
|
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
|
Then switch to the terminal and execute the script by typing,
|
Highlight the output on the terminal
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
|
Slide | From this example, it is evident that:
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 |
|
Slide
# Piece of code to be executed at the end of the PERL script } |
The syntax for END block is as follows
Press Enter
Close curly bracket |
Now let us look at an example of END blocks. | |
Switch to the Terminal and type
|
Open the Terminal and type
|
Gedit
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.
|
Gedit | Let us look at what I have written inside this script.
|
Press Ctrl S | Now, press Ctrl+s to save the file. |
Switch to terminal
|
Then switch to the terminal and execute the script by typing,
|
Highlight the output on the terminal
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
|
Slide | From the example, it is evident that
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.
|
Slide | UNITCHECK, CHECK and INIT blocks are useful-
|
Slide | UNITCHECK and CHECK blocks runs in Last in First out manner
|
Slide
# Piece of code to be executed } |
The syntax for UNITCHECK block is as follows
Press Enter
Press Enter
|
Slide
# Piece of code to be executed } |
The syntax for CHECK block is as follows
Press Enter
Press Enter
|
Slide
# Piece of code to be initialised } |
The syntax for INIT block is as follows
Press Enter
Press Enter
|
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 -
|
Slide: Assignment | Here is assignment for you -
END { @array = (); print "Length of an array inside END block: ", $#array + 1, "\n"; }
@array = (1, 2, 3); }
|
About the Project | Watch the video available at the following link.
|
Spoken Tutorial Workshops | The Spoken Tutorial Project Team
For more details, please write to contact at spoken hyphen tutorial dot org |
Acknowledgment | Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Hope you enjoyed this Perl tutorial.
This is Amol Brahmankar signing off.
|