Difference between revisions of "PERL/C3/Sample-PERL-program/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 53: Line 53:
 
| style="border:1pt solid #000000;padding:0.097cm;"| The sample Perl program will give the output of various weather forecast reports of a region.
 
| style="border:1pt solid #000000;padding:0.097cm;"| The sample Perl program will give the output of various weather forecast reports of a region.
  
'''Weather.pm''' is a''' module''' file that has a complex data structure to hold the data required for this program.
+
*'''Weather.pm''' is a''' module''' file that has a complex data structure to hold the data required for this program.
  
It also contains various '''functions '''to generate the report.
+
*It also contains various '''functions '''to generate the report.
 +
 
 +
*'''Weather underscore report.pl''' is the''' Perl''' program which makes use of this '''module '''file to give the required output.
  
'''Weather''' '''underscore report.pl''' is the''' perl''' program which makes use of this '''module '''file to give the required output.
 
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The same code files are available under this video of our website. Download and unzip the files provided in the code file link.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The same code files are available under this video of our website.  
 +
 
 +
Download and unzip the files provided in the code file link.
  
 
|-
 
|-
Line 67: Line 70:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight the code line by line.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
  
 
| style="border:1pt solid #000000;padding:0.097cm;"| The block of code in this program is under the '''namespace Weather'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| The block of code in this program is under the '''namespace Weather'''.
 +
 
'''Perl '''implements '''namespace '''using the''' package keyword.'''
 
'''Perl '''implements '''namespace '''using the''' package keyword.'''
  
'''BEGIN''' block is compiled and executed before the main program.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
  
'''Export''' allows to export the '''functions''' and variables of '''modules''' to user's '''namespace'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''BEGIN''' block is compiled and executed before the main program.
  
'''At the rate EXPORT''' and''' at the rate EXPORT underscore OK''' are the two main variables used during export operation.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''Export''' allows to export the '''functions''' and variables of '''modules''' to user's '''namespace'''.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''At the rate EXPORT''' and''' at the rate EXPORT underscore OK''' are the two main variables used during export operation.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''At the rate EXPORT''' contains list of '''subroutines''' and variables of the '''module.'''
  
'''At the rate EXPORT''' contains list of '''subroutines''' and variables of the '''module.'''
 
  
 
These will be exported into the caller '''namespace.'''
 
These will be exported into the caller '''namespace.'''
  
'''At the rate EXPORT underscore OK''' does export of symbols on demand basis.
 
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''At the rate EXPORT underscore OK''' does export of symbols on demand basis.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here I have used''' references''' to create complex data structures to hold the data required for a weather report.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here I have used''' references''' to create complex data structures to hold the data required for a weather report.
  
'''$weather_report''' is a''' hash reference'''.
+
*'''$weather_report''' is a''' hash reference'''.
  
“'''place'''” and “'''nstate'''” has the scalar values.
+
*“'''place'''” and “'''nstate'''” has the scalar values.
  
“'''weekly'''” is '''hash''' of''' hash references'''.
+
*“'''weekly'''” is '''hash''' of''' hash references'''.
  
Each week day has four '''keys''' -  
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the code line as per narration.
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
*Each week day has four '''keys''' -  
 +
**'''max underscore temp''',
 +
** '''min underscore temp''',
 +
** '''sunrise''',
 +
** '''sunset.'''
  
'''max underscore temp, min underscore temp, sunrise, sunset.'''
+
*“'''record underscore time'''” is an''' array reference''' with two index values.
  
“'''record underscore time'''” is an''' array reference''' with two index values.
 
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
 
| style="border:1pt solid #000000;padding:0.097cm;"|  
Line 117: Line 145:
 
| style="border:1pt solid #000000;padding:0.097cm;"| This '''function '''prints the daily report on the screen depending upon the weekday input.
 
| style="border:1pt solid #000000;padding:0.097cm;"| This '''function '''prints the daily report on the screen depending upon the weekday input.
  
We retrieve the parameter passed into a '''subroutine''' using the''' shift function.'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight as per narration
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|We retrieve the parameter passed into a '''subroutine''' using the''' shift function.'''
  
 
I have used the''' trim function''' to remove the leading and trailing spaces of the parameter value.
 
I have used the''' trim function''' to remove the leading and trailing spaces of the parameter value.
  
Here is the code for the '''trim''' '''function'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''trim function''' code
  
'''Lc function''' returns a lowercase version of the given input.
+
| style="border:1pt solid #000000;padding:0.097cm;"|Here is the code for the '''trim function'''.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''lc'''
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''Lc function''' returns a lowercase version of the given input.
  
 
This is used to avoid case-sensitivity.
 
This is used to avoid case-sensitivity.
  
The week day passed as parameter from the main program, is assigned to a''' local variable''' '''dollar week underscore day'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''$week_day'''  
  
The following '''print statements''' will print the data corresponding to a specified week day.
+
| style="border:1pt solid #000000;padding:0.097cm;"|The week day -
 +
*passed as parameter from the main program,
 +
*is assigned to a''' local variable dollar week underscore day'''.
  
We are using the '''arrow operator''' to''' dereference''' a value in '''$weather underscore report'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''print statements''' code
  
When working with '''references''', we have to understand the '''data type '''we are '''dereferencing'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"|The following '''print statements''' will print the data corresponding to a specified week day.
  
If it is a '''hash''', we need to pass the '''key '''in curly braces.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''arrow operator'''
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|We are using the '''arrow operator''' to''' dereference''' a value in '''$weather underscore report'''.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|When working with '''references''', we have to understand the '''data type '''we are '''dereferencing'''.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''{$week_day}'''
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|If it is a '''hash''', we need to pass the '''key '''in curly braces.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''[0]'''
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"|If it is an '''array,''' we need to use the square brackets with the index values.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''return''' code
  
If it is an '''array,''' we need to use the square brackets with the index values.
+
| style="border:1pt solid #000000;padding:0.097cm;"|'''return function''' of '''Perl''' returns a value.
  
'''return function''' of '''Perl''' returns a value.
 
  
 
This can be used to check the status of the '''function''' in the main program.
 
This can be used to check the status of the '''function''' in the main program.
Line 152: Line 213:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''>'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| The''' open function''' with the greater than (>) symbol defines the''' WRITE''' mode.
 
| style="border:1pt solid #000000;padding:0.097cm;"| The''' open function''' with the greater than (>) symbol defines the''' WRITE''' mode.
  
Filename is created with the weekday name and '''dot txt '''extension.
 
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| '''Highlight the print statements'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''$week_day.txt'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Filename is created with the weekday name and '''dot txt '''extension.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''print statements'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| The '''print statements''' will print the corresponding data of a specified week day to a file.
 
| style="border:1pt solid #000000;padding:0.097cm;"| The '''print statements''' will print the corresponding data of a specified week day to a file.
  
Line 165: Line 229:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''foreach loop'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| I have declared a '''foreach loop''' to loop through each weekday of the''' hash reference'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| I have declared a '''foreach loop''' to loop through each weekday of the''' hash reference'''.
  
I have used curly brackets to represent the''' hash reference''' and the '''arrow '''operator to''' dereference'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''foreach loop'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| I have used  
 +
*curly brackets to represent the''' hash reference'''  
 +
*and the '''arrow '''operator to''' dereference'''.
  
I am using the''' “keys” built-in function '''to loop through the '''keys '''of the '''hash'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''keys'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|I am using the''' “keys” in-built function '''to loop through the '''keys '''of the '''hash'''.
  
'''Display underscore daily underscore report function '''will print each element of the '''hash'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''display_daily_report'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''display underscore daily underscore report function '''will print each element of the '''hash'''.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to another Perl program '''weather_report.pl'''
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us see a '''Perl '''program '''weather underscore report dot pl '''where we will make use of this '''module '''file '''Weather dot pm'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us see -
 +
*a '''Perl '''program '''weather underscore report dot pl '''
 +
*where we will make use of this '''module '''file '''Weather dot pm'''.
  
 
|-
 
|-
Line 183: Line 257:
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, '''use strict''' and''' use warnings''' are compiler flags that helps to avoid common programming mistakes.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Here, '''use strict''' and''' use warnings''' are compiler flags that helps to avoid common programming mistakes.
  
'''use Weather semicolon.'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''use Weather;'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''use Weather semicolon.'''
  
 
Here '''Weather '''is a''' module''' name which I have used in this program.
 
Here '''Weather '''is a''' module''' name which I have used in this program.
Line 192: Line 268:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the '''print statements'''.
 
| style="border:1pt solid #000000;padding:0.097cm;"| In this program, I'll print different reports depending upon the given options.
 
| style="border:1pt solid #000000;padding:0.097cm;"| In this program, I'll print different reports depending upon the given options.
  
Line 201: Line 277:
 
# weekly weather report
 
# weekly weather report
  
If option 1 is typed, it will ask the user to enter the day of a week.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight code for 1 option
 +
| style="border:1pt solid #000000;padding:0.097cm;"| If option '''1''' is typed, it will ask the user to enter the day of a week.
  
The '''diamond '''operator will read from''' STDIN''', that is from the keyboard.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''<>'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| The '''diamond '''operator will read from''' STDIN''', that is from the keyboard.
  
 
For example, if the user enters ''''monday'''', then it is assigned to a variable '''dollar dayoption, '''which is a''' local variable'''.
 
For example, if the user enters ''''monday'''', then it is assigned to a variable '''dollar dayoption, '''which is a''' local variable'''.
  
Next, we can see that we are calling two functions''' display_header()''' and''' display_daily_report().'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight code for '''display_weather''' and '''display_daily_report'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Next, we can see that we are calling two functions-
 +
*''' display_header()''' and
 +
*''' display_daily_report().'''
  
We have exported all '''functions''' in '''Weather.pm''' with “'''use Weather'''” statement in this file.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We have exported all '''functions''' in '''Weather.pm''' with “'''use Weather'''” statement in this file.
  
 
So, no need to refer the '''functions''' within a package using the '''colon colon (::)package qualifier'''
 
So, no need to refer the '''functions''' within a package using the '''colon colon (::)package qualifier'''
Line 218: Line 304:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight code for option 2
 
| style="border:1pt solid #000000;padding:0.097cm;"| If option 2 is typed, it will prompt the user to enter the day of a week.
 
| style="border:1pt solid #000000;padding:0.097cm;"| If option 2 is typed, it will prompt the user to enter the day of a week.
  
'''$dayoption''' is passed as the input parameter to the '''function write underscore daily underscore report.'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''$dayoption'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''$dayoption''' is passed as the input parameter to the '''function write underscore daily underscore report.'''
  
'''return '''value from the '''function''' is stored in the variable '''dollar result'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''$result'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''return '''value from the '''function''' is stored in the variable '''dollar result'''.
  
'''Print statement''' asks the user to check the text file for the output.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''print''' statement
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''Print statement''' asks the user to check the text file for the output.
  
The filename is created with the day of the week dot txt as output file.
+
The filename is created with the day of the week '''dot txt''' as output file.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight code for option 3
 
| style="border:1pt solid #000000;padding:0.097cm;"| If option 3 is typed, it prints the weather report for the whole week.
 
| style="border:1pt solid #000000;padding:0.097cm;"| If option 3 is typed, it prints the weather report for the whole week.
  
'''Display underscore weekly underscore report''' is the '''function''' name of the weekly report.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''display_weekly_report'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|'''display underscore weekly underscore report''' is the '''function''' name of the weekly report.
  
This print statement draws a horizontal line for the specified number of times.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''print''' statement
 +
| style="border:1pt solid #000000;padding:0.097cm;"|This '''print''' statement draws a horizontal line for the specified number of times.
  
 
This is just to give a good look to the report.
 
This is just to give a good look to the report.
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight code for option 4
 
| style="border:1pt solid #000000;padding:0.097cm;"| Lastly, if the option is 4, it will quit the program.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Lastly, if the option is 4, it will quit the program.
  
If any option, other than the ones specified is given, the''' print statement''' says “'''Incorrect option'''”.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''elseif'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| If any option, other than the ones specified, is given, the''' print statement''' says “'''Incorrect option'''”.
  
Here the exit value of 0 indicates the program ran successfully.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight '''exit(0)'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Here the exit value of 0 indicates the program ran successfully.
  
 
The exit value other than 0 means an error of some kind has occurred.  
 
The exit value other than 0 means an error of some kind has occurred.  
Line 273: Line 373:
 
4.Exit
 
4.Exit
  
Enter the Option: 1
+
'''Enter the Option:''' 1
  
 
'''>>'''press''' Enter.'''
 
'''>>'''press''' Enter.'''
Line 281: Line 381:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Enter a day: monday
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter a day:''' >> Type '''monday'''
  
 
'''>>'''press''' Enter.'''
 
'''>>'''press''' Enter.'''
Line 297: Line 397:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Press up-arrow key
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now I'll again execute the program once again to demonstrate the other options.
 
| style="border:1pt solid #000000;padding:0.097cm;"| Now I'll again execute the program once again to demonstrate the other options.
  
Line 309: Line 409:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| 1.Daily Weather Report
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter the Option:''' >> Type 2 >>press''' Enter.'''
 
+
2.Daily Weather Report to File
+
 
+
3.Weekly Weather Report
+
 
+
4.Exit
+
 
+
Enter the Option: 2
+
 
+
'''>>'''press''' Enter.'''
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''2 '''and press '''Enter.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''2 '''and press '''Enter.'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| Enter a day: wednesday
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter a day:''' >> Type '''wednesday'''
  
 
'''>>'''press''' Enter.'''
 
'''>>'''press''' Enter.'''
Line 359: Line 449:
 
| style="border:1pt solid #000000;padding:0.097cm;"| On the terminal type
 
| style="border:1pt solid #000000;padding:0.097cm;"| On the terminal type
  
'''perl weather_report. Pl'''
+
'''perl weather_report. Pl''' >> press''' Enter.'''
 
+
'''>>'''press''' Enter.'''
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the terminal and type''' perl weather underscore report dot pl '''and press''' Enter'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Switch to the terminal and type''' perl weather underscore report dot pl '''and press''' Enter'''
  
 
|-
 
|-
| style="border:1pt solid #000000;padding:0.097cm;"| 1.Daily Weather Report
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Enter the Option:''' 3 >> press''' Enter.'''
 
+
2.Daily Weather Report to File
+
 
+
3.Weekly Weather Report
+
 
+
4.Exit
+
 
+
Enter the Option: 3
+
 
+
'''>>'''press''' Enter.'''
+
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''3 '''and press '''Enter.'''
 
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''3 '''and press '''Enter.'''
  

Latest revision as of 17:05, 9 November 2015

Title of script: Sample PERL Program

Author: Nirmala Venkat

Keywords: module, package, array, hash, functions, reference, dereference, PERL, PERL programming, video tutorial


Visual Cue
Narration
Slide 1: Welcome to the Spoken Tutorial on Sample PERL program.
Slide 2:

Learning objectives

In this tutorial we will learn to
  • include all the major topics we covered so far
  • in a sample Perl program
Slide 3:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2
  • and the gedit Text Editor

You can use any text editor of your choice.

Slide 4:

Pre-requisites

As a prerequisite, you should have working knowledge of Perl Programming.

If not, then go through the relevant Perl spoken tutorials on this website.

Slide 5:

Program outline:


The sample Perl program will give the output of various weather forecast reports of a region.
  • Weather.pm is a module file that has a complex data structure to hold the data required for this program.
  • It also contains various functions to generate the report.
  • Weather underscore report.pl is the Perl program which makes use of this module file to give the required output.
The same code files are available under this video of our website.

Download and unzip the files provided in the code file link.

Switch to the Weather.pm in gedit Now let us see our sample Perl program Weather dot pm.
Highlight the code line as per narration. The block of code in this program is under the namespace Weather.

Perl implements namespace using the package keyword.

Highlight the code line as per narration. BEGIN block is compiled and executed before the main program.
Highlight the code line as per narration. Export allows to export the functions and variables of modules to user's namespace.
Highlight the code line as per narration. At the rate EXPORT and at the rate EXPORT underscore OK are the two main variables used during export operation.
Highlight the code line as per narration. At the rate EXPORT contains list of subroutines and variables of the module.


These will be exported into the caller namespace.

Highlight the code line as per narration. At the rate EXPORT underscore OK does export of symbols on demand basis.
Highlight the code line as per narration. Here I have used references to create complex data structures to hold the data required for a weather report.
  • $weather_report is a hash reference.
  • place” and “nstate” has the scalar values.
  • weekly” is hash of hash references.
Highlight the code line as per narration.
  • Each week day has four keys -
    • max underscore temp,
    • min underscore temp,
    • sunrise,
    • sunset.
  • record underscore time” is an array reference with two index values.
I have a few subroutines to display the weather report of various options.

Let us see one by one.

Highlight display_header() This function prints the header information such as header of the report, place, state and current date.
Now let us see the next function display underscore daily underscore report.
Highlight display_daily_report() This function prints the daily report on the screen depending upon the weekday input.
Highlight as per narration We retrieve the parameter passed into a subroutine using the shift function.

I have used the trim function to remove the leading and trailing spaces of the parameter value.

Highlight the trim function code Here is the code for the trim function.
Highlight lc Lc function returns a lowercase version of the given input.

This is used to avoid case-sensitivity.

Highlight $week_day The week day -
  • passed as parameter from the main program,
  • is assigned to a local variable dollar week underscore day.
Highlight the print statements code The following print statements will print the data corresponding to a specified week day.
Highlight arrow operator We are using the arrow operator to dereference a value in $weather underscore report.
When working with references, we have to understand the data type we are dereferencing.
Highlight {$week_day} If it is a hash, we need to pass the key in curly braces.
Highlight [0] If it is an array, we need to use the square brackets with the index values.
Highlight return code return function of Perl returns a value.


This can be used to check the status of the function in the main program.

The next function is write underscore daily underscore report.
Highlight write_daily_report() This function will print the report output to a file.
Highlight > The open function with the greater than (>) symbol defines the WRITE mode.
Highlight $week_day.txt Filename is created with the weekday name and dot txt extension.
Highlight the print statements The print statements will print the corresponding data of a specified week day to a file.
Highlight display_weekly_report() This prints the weekly report.
Highlight foreach loop I have declared a foreach loop to loop through each weekday of the hash reference.
Highlight foreach loop I have used
  • curly brackets to represent the hash reference
  • and the arrow operator to dereference.
Highlight keys I am using the “keys” in-built function to loop through the keys of the hash.
Highlight display_daily_report display underscore daily underscore report function will print each element of the hash.
Switch to another Perl program weather_report.pl Now let us see -
  • a Perl program weather underscore report dot pl
  • where we will make use of this module file Weather dot pm.
Highlight the code according to the narration. Here, use strict and use warnings are compiler flags that helps to avoid common programming mistakes.
Highlight use Weather; use Weather semicolon.

Here Weather is a module name which I have used in this program.

We have already seen that the functions required for this program have been stored in this module.

It is not required to give the .pm file extension here.

Highlight the print statements. In this program, I'll print different reports depending upon the given options.

The user has to enter an option to print the

  1. daily weather report of a particular week day
  2. daily weather report of a particular week day to an output file
  3. weekly weather report
Highlight code for 1 option If option 1 is typed, it will ask the user to enter the day of a week.
Highlight <> The diamond operator will read from STDIN, that is from the keyboard.

For example, if the user enters 'monday', then it is assigned to a variable dollar dayoption, which is a local variable.

Highlight code for display_weather and display_daily_report Next, we can see that we are calling two functions-
  • display_header() and
  • display_daily_report().
We have exported all functions in Weather.pm with “use Weather” statement in this file.

So, no need to refer the functions within a package using the colon colon (::)package qualifier

Now let's see the next option.
Highlight code for option 2 If option 2 is typed, it will prompt the user to enter the day of a week.
Highlight $dayoption $dayoption is passed as the input parameter to the function write underscore daily underscore report.
Highlight $result return value from the function is stored in the variable dollar result.
Highlight print statement Print statement asks the user to check the text file for the output.

The filename is created with the day of the week dot txt as output file.

Highlight code for option 3 If option 3 is typed, it prints the weather report for the whole week.
Highlight display_weekly_report display underscore weekly underscore report is the function name of the weekly report.
Highlight print statement This print statement draws a horizontal line for the specified number of times.

This is just to give a good look to the report.

Highlight code for option 4 Lastly, if the option is 4, it will quit the program.
Highlight elseif If any option, other than the ones specified, is given, the print statement says “Incorrect option”.
Highlight exit(0) Here the exit value of 0 indicates the program ran successfully.

The exit value other than 0 means an error of some kind has occurred.

Now let us execute the program.
On the terminal type

perl weather_report. Pl

>>press Enter.

Switch to the terminal and type

perl weather underscore report dot pl and press Enter

1.Daily Weather Report

2.Daily Weather Report to File

3.Weekly Weather Report

4.Exit

Enter the Option: 1

>>press Enter.

We can see four options on the screen.

Type 1 and press Enter.

Enter a day: >> Type monday

>>press Enter.

We are prompted to enter a day of the week.

I'll type 'monday' and press Enter.

Highlight the header of the report This is the header output generated from the function display underscore header().
Highlight the monday report Now, we can see the weather report of Monday.
Press up-arrow key Now I'll again execute the program once again to demonstrate the other options.
On the terminal type

perl weather_report. Pl

>>press Enter.

Enter the Option: >> Type 2 >>press Enter. Type 2 and press Enter.
Enter a day: >> Type wednesday

>>press Enter.

At the prompt, we have to type any week day.

I will type ‘wednesday’ and press Enter.

We can see a message “Please check the file wednesday dot txt for report output ”.
The output has been written to this text file.

Let us open the file and check the content.

On the terminal type

gedit wednesday.txt

>>press Enter.

Type gedit wednesday dot txt and press Enter.
Highlight the output The output file has been created with the entered week day name with txt extension.
Now let us check the next option.
On the terminal type

perl weather_report. Pl >> press Enter.

Switch to the terminal and type perl weather underscore report dot pl and press Enter
Enter the Option: 3 >> press Enter. Type 3 and press Enter.
Highlight the output This time, we can see the weekly weather report.
The hash keys and hash values are stored in a random order.

So the displayed output is not related to the order in which they were added.

With this, we come to the end of this tutorial.

Let us summarize.

Slide 6: In this tutorial we have seen a sample Perl program by covering main topics of our previous tutorials.
Slide 7:

Assignment

As an assignment,
  1. Write a similar Perl program employee underscore report.pl for displaying employee salary, designation, department, leave_balance details.
  2. Pass Employee ID or Employee name as input.
  3. Write the required functions in the module Employee.pm file.
Slide 8:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it

Slide 9:

Spoken Tutorial workshops

We conduct workshops and give certificates for those who pass our online tests.

For more details, please write to us.

Slide 10:

Acknowledgement

Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat