Difference between revisions of "PERL/C2/Blocks-in-Perl/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title Of Script: '''BLOCKS in PERL '''Author:''' Amol Brahmankar '''Keywords:''' BLOCKS in perl video tutorial. {| style="border-spacing:0;" | style="border-top:0.0007in …')
 
 
(One intermediate revision by one other user not shown)
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 Clue'''</center>
+
| 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>
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on BLOCKS in '''Perl'''
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on '''BLOCKS''' in '''Perl'''.
  
 
|-
 
|-
Line 25: Line 25:
  
 
I will also be using the '''gedit '''Text Editor.
 
I will also be using the '''gedit '''Text Editor.
 
  
 
You can use any text editor of your choice.
 
You can use any text editor of your choice.
Line 31: Line 30:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| As a pre-requisite,
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| As a pre-requisite, you should have basic knowledge of '''Variables, Comments '''in''' Perl'''
  
you should have basic knowledge of '''Variables, Comments '''in''' Perl'''
 
  
 
+
Knowledge of''' data structures''' in '''PERL''' will be an added advantage.
Knowledge of''' data structures in PERL '''will be an added advantage'''.'''
+
  
  
Line 43: Line 40:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Perl provides 5 special '''blocks'''.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Perl''' provides 5 special '''blocks'''.
  
  
Line 50: Line 47:
  
 
These blocks are:
 
These blocks are:
 
 
# BEGIN
 
# BEGIN
 
# END
 
# END
Line 56: Line 52:
 
# CHECK
 
# CHECK
 
# INIT
 
# INIT
 
  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us start with understanding the '''BEGIN''' block
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us start with understanding the '''BEGIN''' block.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''BEGIN''' block get executed at the time of compilation.  
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|  
 +
* '''BEGIN''' block get executed at the time of compilation.  
 
* So, any code written inside this block gets executed first during compilation.
 
* So, any code written inside this block gets executed first during compilation.
 
* We can have several '''BEGIN''' blocks inside a '''Perl '''script.
 
* We can have several '''BEGIN''' blocks inside a '''Perl '''script.
 
* These blocks will get executed in the order of declaration.
 
* These blocks will get executed in the order of declaration.
 
* That is in the '''First define First execute '''pattern
 
* That is in the '''First define First execute '''pattern
 
 
  
 
|-
 
|-
Line 87: Line 81:
 
'''BEGIN''' in capital letters space open curly bracket
 
'''BEGIN''' in capital letters space open curly bracket
  
'''Press Enter'''
+
Press '''Enter'''.
  
  
 
Piece of code to be executed at the time of compilation
 
Piece of code to be executed at the time of compilation
  
'''Press Enter'''
+
Press '''Enter'''.
 +
 
  
 
Close curly bracket
 
Close curly bracket
Line 98: Line 93:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us look at an example of '''BEGIN '''blocks.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us look at an example of '''BEGIN '''blocks.
  
 
|-
 
|-
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to the Terminal and type
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to the '''Terminal''' and type
  
  
'''gedit beginBlock.pl '''and press '''Enter'''.
+
'''gedit beginBlock.pl &'''and press '''Enter'''.
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type
  
Line 111: Line 106:
  
  
and press''' Enter'''
+
and press''' Enter'''.
  
 
|-
 
|-
Line 140: Line 135:
  
 
'''print "Last line in PERL script\n";'''
 
'''print "Last line in PERL script\n";'''
 +
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''beginBlock dot pl''' file in '''gedit'''.
  
  
 +
Type the following piece of code as displayed on the screen.
  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''beginBlock dot pl''' file in '''gedit'''.
 
  
  
Type the following piece of code as displayed on the screen.
 
  
  
Line 156: Line 151:
  
  
* Here we have printed some text before and after the '''BEGIN''' blocks.<br/>
+
Here, we have printed some text before and after the '''BEGIN''' blocks.
  
* Similarly, I have written one '''print statement''' in each '''BEGIN''' block.<br/>
+
|-
 
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|In '''Gedit'''text editor
* Please note, I have '''not''' given the '''semicolon''' after the '''BEGIN''' blocks.<br/>
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|Similarly, I have written one '''print statement''' in each '''BEGIN''' block.
 
+
* Putting a semicolon, will result in a '''syntax error''' on execution of the program.
+
  
 +
|-
 +
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|In '''Gedit'''text editor
 +
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|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.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''ctrl+s''' to '''save''' the file.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''Ctrl+s''' to save the file.
  
 
|-
 
|-
Line 175: Line 172:
  
 
'''perl beginBlock.pl'''
 
'''perl beginBlock.pl'''
 
 
 
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and execute the script by typing,
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and execute the script by typing,
  
Line 200: Line 194:
 
Last line in PERL script  
 
Last line in PERL script  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the '''terminal'''
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the '''terminal'''
 +
 +
  
  
Line 206: Line 202:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Notice that;'''
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|Notice that
  
* The line written inside the first '''BEGIN''' '''block '''gets''' '''printed first and  
+
* 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.'''
 
* 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.
 
* '''BEGIN blocks''' gets executed in the order of their declaration.
 
 
  
 
|-
 
|-
Line 218: Line 212:
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From this example, it is evident that:
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From this example, it is evident that:
  
* The code written inside the '''BEGIN''' blocks get executed '''first'''.
+
* The code written inside the '''BEGIN''' blocks gets executed '''first'''.
 
* This is irrespective of the location of the '''BEGIN''' block inside '''PERL''' script.
 
* 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
+
* '''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.
 
So one of the use of this '''block''' is to include files inside a '''Perl '''script, before actual execution starts.
Line 230: Line 224:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''END''' block get executed at the end of the '''PERL''' program
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"|  
* Code written inside this '''block''' gets executed after '''PERL''' has finished executing the program
+
* '''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.
 
* We can have several '''END''' blocks inside a '''Perl''' script.
* These '''blocks''' will get executed in reverse order of definition.
+
* These '''blocks''' will get executed in reverse order of declaration
* That is in '''Last define First execute''' pattern.
+
* That is, in '''Last define First execute''' pattern.
 
+
 
+
  
 
|-
 
|-
Line 247: Line 240:
  
 
'''}'''
 
'''}'''
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''END''' block''' '''is as follows  
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''END''' block is as follows  
  
  
 
'''END''' in capital letters space open curly bracket
 
'''END''' in capital letters space open curly bracket
  
'''Press Enter'''
+
Press '''Enter'''
  
  
Piece of code to be executed at the end of the PERL script
+
Piece of code to be executed at the end of the '''PERL''' script
  
  
'''Press Enter'''
+
Press '''Enter'''
  
 
Close curly bracket
 
Close curly bracket
Line 270: Line 263:
  
  
'''gedit endBlock.pl '''and press '''Enter'''.
+
'''gedit endBlock.pl &'''and press '''Enter'''.
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type
  
Line 306: Line 299:
  
 
'''print "Last line in PERL script\n";'''
 
'''print "Last line in PERL script\n";'''
 
 
  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''endBlock dot pl''' file in '''gedit'''.
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''endBlock dot pl''' file in '''gedit'''.
Line 313: Line 304:
  
 
Type the following piece of code as displayed on the screen.
 
Type the following piece of code as displayed on the screen.
 +
 +
  
  
Line 322: Line 315:
  
  
* Here we have printed some text before and after the '''END''' blocks.<br/>
+
* Here we have printed some text before and after the '''END''' blocks.
 
+
* Similarly, we have written one print statement in each '''END''' block.<br/>
+
 
+
* Please note, I have not given the semicolon after the '''END''' block.<br/>
+
  
* If we give the semicolon, there will be a syntax error on compilation.
+
* 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.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''ctrl+s''' to '''save''' the file.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''Ctrl+s''' to save the file.
  
 
|-
 
|-
Line 365: Line 356:
  
 
Inside First END block  
 
Inside First END block  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the terminal
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the '''terminal'''.
 +
 
 +
 
 +
 
  
  
Line 372: Line 366:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Notice that;
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Notice that  
  
 
* The line written inside the '''END''' block is printed last.
 
* 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
+
* 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
+
* '''END blocks''' gets executed in the reverse order of their declaration.
 
+
 
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From the example, it is evident that;
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From the example, it is evident that  
  
* The code written inside the '''END''' blocks get executed at the end
+
* 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  
 
* 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
+
* '''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.
 
So, one use of''' END block''' is to destroy objects created in the program, before exiting.
Line 392: Line 384:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Similarly '''PERL''' has '''UNITCHECK, CHECK''' and '''INIT''' blocks.
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Similarly, '''PERL''' has '''UNITCHECK, CHECK''' and '''INIT''' blocks.
  
  
Line 398: Line 390:
  
  
So, I will be just brief you about these blocks.
+
So, I will be just briefing you about these blocks.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''UNITCHECK, CHECK '''and '''INIT''' blocks are useful;
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''UNITCHECK, CHECK '''and '''INIT''' blocks are useful-
  
 
* to catch the '''transition''' between '''compilation''' and '''execution''' '''phase''' of the main program and  
 
* 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  
+
* to perform some '''checks''' or '''initialisation''', after compilation and before execution  
 
+
 
+
  
 
|-
 
|-
Line 437: Line 427:
  
 
Piece of code to be executed
 
Piece of code to be executed
 
  
 
Press''' Enter'''
 
Press''' Enter'''
 +
  
 
Close curly bracket
 
Close curly bracket
Line 461: Line 451:
  
 
Piece of code to be executed
 
Piece of code to be executed
 
  
 
Press''' Enter'''
 
Press''' Enter'''
 +
  
 
Close curly bracket
 
Close curly bracket
Line 485: Line 475:
  
 
Piece of code to be initialised
 
Piece of code to be initialised
 
  
 
Press''' Enter'''
 
Press''' Enter'''
 +
  
 
Close curly bracket
 
Close curly bracket
Line 493: Line 483:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"|  
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For better understanding, I recommend that you experiment with these blocks in your Perl scripts.  
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For better understanding, I recommend that you experiment with these blocks in your '''Perl''' scripts.  
  
 
|-
 
|-
Line 501: Line 491:
 
In this tutorial, we have learnt -
 
In this tutorial, we have learnt -
  
* '''BEGIN '''and''' END '''blocks''' '''in''' details '''and<br/>
+
* '''BEGIN '''and''' END '''blocks in detail and  
 
+
* '''Introduction '''to''' UNITCHECK, CHECK '''and''' INIT '''blocks''' '''<br/>
+
 
+
* using sample program
+
  
 +
* '''Introduction '''to''' UNITCHECK, CHECK '''and''' INIT '''blocks
  
 +
* using sample programs
  
 
|-
 
|-
Line 538: Line 526:
  
  
execute the script and observe the output.
+
Execute the script and observe the output.
 
+
 
+
 
+
  
 
|-
 
|-
Line 548: Line 533:
  
  
It summaries the Spoken Tutorial project
+
It summaries the Spoken Tutorial project.
  
  
If you do not have good bandwidth, you can
+
If you do not have good bandwidth, you can download and watch it.
 
+
download and watch it
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team  
 
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team  
 
  
 
* Conducts workshops using spoken tutorials
 
* Conducts workshops using spoken tutorials
 
* Gives certificates to those who pass an online test
 
* Gives certificates to those who pass an online test
  
For more details, please write to
+
For more details, please write to contact at spoken hyphen tutorial dot org
 
+
contact at spoken hyphen tutorial dot org
+
  
 
|-
 
|-
Line 572: Line 552:
  
 
http://spoken-tutorial.org\NMEICT-Intro
 
http://spoken-tutorial.org\NMEICT-Intro
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a  
+
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
 
+
Teacher project
+
 
+
 
+
It is supported by the National Mission on
+
  
Education through ICT, MHRD, Government of India.
 
  
 +
It is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
More information on this Mission is available
 
  
spoken hyphen tutorial dot org slash NMEICT hyphen Intro
+
More information on this Mission is available at spoken hyphen tutorial dot org slash NMEICT hyphen Intro
  
 
|-
 
|-

Latest revision as of 13:02, 17 June 2014

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