PERL/C3/Sample-PERL-program/English-timed
From Script | Spoken-Tutorial
Revision as of 17:24, 27 December 2015 by Sandhya.np14 (Talk | contribs)
00:01 | Welcome to the Spoken Tutorial on Sample PERL program. |
00:06 | In this tutorial, we will learn to include all the major topics we covered so far
in a sample Perl program. |
00:14 | To record this tutorial, I am using:
|
00:25 | You can use any text editor of your choice. |
00:29 | As a prerequisite, you should have working knowledge of Perl programming. |
00:34 | If not, then go through the relevant Perl spoken tutorials on this website. |
00:39 | The sample Perl program will give the output of various weather forecast reports of a region. |
00:46 | Weather dot pm is a module file that has a complex data-structure to hold the data required for this program. |
00:54 | It also contains various functions to generate the report. |
00:59 | Weather underscore report dot pl is the Perl program which makes use of this module file to give the required output. |
01:08 | The same code files are available under this video on our website. |
01:13 | Download and unzip the files provided in the code file link. |
01:18 | Now, let us see our sample Perl program Weather dot pm. |
01:24 | The block of code in this program is under the namespace Weather. |
01:29 | Perl implements namespace using the package keyword. |
01:34 | BEGIN block is compiled and executed before the main program. |
01:40 | Export allows to export the functions and variables of modules to user's namespace. |
01:48 | At the rate EXPORT and at the rate EXPORT underscore OK are the two main variables used during export operation. |
01:57 | At the rate EXPORT contains list of subroutines and variables of the module. |
02:03 | These will be exported into the caller namespace. |
02:07 | At the rate EXPORT underscore OK does export of symbols on demand basis. |
02:14 | Here, I have used references to create complex data-structures to hold the data required for a weather report. |
02:24 | $weather_report is a hash reference. “place” and “nstate” have the scalar values. |
02:32 | “weekly” is hash of hash references. |
02:37 | *Each week day has four keys -
max underscore temp, min underscore temp, sunrise, sunset. |
02:48 | “record underscore time” is an array reference with two index values. |
02:54 | I have a few subroutines to display the weather report of various options.
Let us see one by one. |
03:01 | This function prints the header information such as header of the report, place, state and current date. |
03:10 | Now, let us see the next function display underscore daily underscore report. |
03:16 | This function prints the daily report on the screen, depending upon the weekday input. |
03:22 | We retrieve the parameter passed into a subroutine using the shift function. |
03:27 | I have used the trim() function to remove the leading and trailing spaces of the parameter value. |
03:34 | Here is the code for the trim() function. |
03:37 | Lc() function returns a lowercase version of the given input. |
03:42 | This is used to avoid case-sensitivity. |
03:45 | The 'week day'- passed as parameter from the main program, is assigned to a local variable dollar week underscore day. |
03:55 | The following print statements will print the data corresponding to a specified week day. |
04:01 | We are using the arrow operator to dereference a value in $weather underscore report. |
04:09 | When working with references, we have to understand the data type we are dereferencing. |
04:15 | If it is a hash, we need to pass the key in curly braces. |
04:20 | If it is an array, we need to use the square brackets with the index values. |
04:26 | return function of Perl returns a value. |
04:29 | This can be used to check the status of the function in the main program. |
04:36 | The next function is write underscore daily underscore report. |
04:40 | This function will print the report output to a file. |
04:45 | The open function with the greater than (>) symbol defines the WRITE mode. |
04:50 | Filename is created with the weekday name and dot txt extension. |
04:56 | The print statements will print the corresponding data of a specified week day to a file. |
05:02 | This prints the weekly report. |
05:05 | I have declared a foreach loop to loop through each weekday of the hash reference. |
05:11 | I have used curly brackets to represent the hash reference and the arrow operator to dereference. |
05:18 | I am using the “keys” in-built function to loop through the keys of the hash. |
05:23 | display underscore daily underscore report function will print each element of the hash. |
05:30 | Now, let us see - a Perl program weather underscore report dot pl where we will make use of this module file Weather dot pm. |
05:40 | Here, use strict and use warnings are compiler flags that help to avoid common programming mistakes. |
05:48 | use Weather semicolon. Here, Weather is a module name which I have used in this program. |
05:56 | We have already seen that the functions required for this program have been stored in this module. |
06:03 | It is not required to give the dot pm file extension here. |
06:08 | In this program, I'll print different reports depending upon the given options. |
06:14 | The user has to enter an option to print the daily weather report of a particular week day,
daily weather report of a particular week day to an output file, weekly weather report. |
06:27 | If option '1' is typed, it will ask the user to enter the day of a week. |
06:32 | The diamond operator will read from STDIN, that is, from the keyboard. |
06:38 | For example, if the user enters 'monday', then it is assigned to a variable dollar dayoption, which is a local variable. |
06:47 | Next, we can see that we are calling two functions-
|
06:56 | We have exported all functions in Weather dot pm with “use Weather” statement in this file. |
07:03 | So, no need to refer the functions within a package using the colon colon (::)package qualifier |
07:10 | Now let's see the next option. |
07:13 | If option '2' is typed, it will prompt the user to enter the day of a week. |
07:19 | $dayoption is passed as the input parameter to the function write underscore daily underscore report. |
07:27 | return value from the function is stored in the variable dollar result. |
07:33 | Print statement asks the user to check the text file for the output. |
07:38 | The filename is created with the day of the week dot txt as output file. |
07:46 | If option '3' is typed, it prints the weather report for the whole week. |
07:51 | display underscore weekly underscore report is the function name of the weekly report. |
07:57 | This print statement draws a horizontal line for the specified number of times. |
08:02 | This is just to give a good look to the report. |
08:06 | Lastly, if the option is 4, it will quit the program. |
08:11 | If any option other than the ones specified is given, the print statement says “Incorrect option”. |
08:19 | Here, the exit value of '0' indicates the program ran successfully. |
08:25 | The exit value other than '0' means an error of some kind has occurred. |
08:31 | Now, let us execute the program. |
08:34 | Switch to the terminal and type perl weather underscore report dot pl and press Enter. |
08:41 | We can see four options on the screen. |
08:45 | Type '1 'and press Enter. |
08:48 | We are prompted to enter a day of the week. I'll type "monday" and press Enter. |
08:56 | This is the header output generated from the function display underscore header(). |
09:02 | Now, we can see the weather report of Monday. |
09:06 | Now I'll again execute the program once again to demonstrate the other options. |
09:13 | Type 2 and press Enter. |
09:17 | At the prompt, we have to type any week day. I will type "wednesday" and press Enter. |
09:25 | We can see a message: Please check the file wednesday dot txt for report output . |
09:32 | The output has been written to this text file. Let us open the file and check the contents. |
09:38 | Type: gedit wednesday dot txt and press Enter. |
09:44 | The output file has been created with the entered week day name with 'txt' extension. |
09:51 | Now, let us check the next option. |
09:54 | Switch to the terminal and type: perl weather underscore report dot pl and press Enter. |
10:00 | Type '3 'and press Enter. |
10:04 | This time, we can see the weekly weather report. |
10:08 | The hash keys and hash values are stored in a random order. |
10:13 | So, the displayed output is not related to the order in which they were added. |
10:19 | With this, we come to the end of this tutorial. Let us summarize. |
10:24 | In this tutorial, we have seen a sample Perl program by covering main topics of our previous tutorials. |
10:32 | As an assignment, write a similar Perl program employee underscore report.pl for displaying employee salary, designation, department, leave_balance details. |
10:45 | Pass Employee ID or Employee name as input. |
10:50 | Write the required functions in the module Employee dot pm file. |
10:56 | The video at the following link summarize the spoken tutorial. Please download and watch it. |
11:03 | We conduct workshops and give certificates for those who pass our online tests.
For more details, please write to us. |
11:12 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
11:25 | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |