PERL/C3/Sample-PERL-program/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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