PERL/C3/Sample-PERL-program/English
Title of script: Sample PERL Program
Author: Nirmala Venkat
Keywords: module, package, array, hash, functions, reference, dereference, PERL, PERL programming, video tutorial
|
|
Slide 1: | Welcome to the Spoken Tutorial on Sample PERL program. |
Slide 2:
Learning objectives |
In this tutorial we will learn to
|
Slide 3:
System Requirements |
To record this tutorial, I am using
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 by line. | The block of code in this program is under the namespace Weather.
Perl implements namespace using the package keyword. BEGIN block is compiled and executed before the main program. Export allows to export the functions and variables of modules to user's namespace. At the rate EXPORT and at the rate EXPORT underscore OK are the two main variables used during export operation. At the rate EXPORT contains list of subroutines and variables of the module. These will be exported into the caller namespace. At the rate EXPORT underscore OK does export of symbols on demand basis. |
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. 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.
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. Here is the code for the trim function. Lc function returns a lowercase version of the given input. 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. The following print statements will print the data corresponding to a specified week day. 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. If it is a hash, we need to pass the key in curly braces. If it is an array, we need to use the square brackets with the index values. 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. |
The open function with the greater than (>) symbol defines the WRITE mode.
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. |
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. I am using the “keys” built-in function to loop through the keys of the hash. Display underscore daily underscore report function will print each element of the hash. | |
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.
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. |
In this program, I'll print different reports depending upon the given options.
The user has to enter an option to print the
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. 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(). 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. | |
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. 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. The filename is created with the day of the week dot txt as output file. | |
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. This print statement draws a horizontal line for the specified number of times. This is just to give a good look to the report. | |
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”. 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: 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. |
Now I'll again execute the program once again to demonstrate the other options. | |
On the terminal type
perl weather_report. Pl >>press Enter. |
|
1.Daily Weather Report
2.Daily Weather Report to File 3.Weekly Weather Report 4.Exit Enter the Option: 2 >>press Enter. |
Type 2 and press Enter. |
Enter a day: 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 |
1.Daily Weather Report
2.Daily Weather Report to File 3.Weekly Weather Report 4.Exit 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,
|
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. |