Difference between revisions of "PERL/C2/Blocks-in-Perl/English"
(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 …') |
Nancyvarkey (Talk | contribs) |
||
| Line 13: | Line 13: | ||
|- | |- | ||
| 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''' |
| − | |||
| − | + | 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'''. |
Piece of code to be executed at the time of compilation | Piece of code to be executed at the time of compilation | ||
| − | ''' | + | 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. | ||
| − | |||
| − | |||
| Line 156: | Line 151: | ||
| − | + | Here, we have printed some text before and after the '''BEGIN''' blocks. | |
| − | + | |- | |
| − | + | | 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;"|Similarly, I have written one '''print statement''' in each '''BEGIN''' block. | |
| − | + | ||
| − | + | ||
| + | |- | ||
| + | | 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 ''' | + | | 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;"| | + | | 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 | + | * 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 | + | * 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 | + | * 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 | + | | 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''' |
| − | 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''' |
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. | + | * 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. | ||
|- | |- | ||
| 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 ''' | + | | 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''' | + | * 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 | + | 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 | + | * '''BEGIN '''and''' END '''blocks in detail and |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | * '''Introduction '''to''' UNITCHECK, CHECK '''and''' INIT '''blocks | ||
| + | * using sample programs | ||
|- | |- | ||
| Line 538: | Line 526: | ||
| − | + | 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. | ||
| − | |||
| − | 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 |
|- | |- | ||
Revision as of 10:40, 11 April 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.
|