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