Difference between revisions of "PERL/C3/Including-files-or-modules/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 120: | Line 120: | ||
|- | |- | ||
|02:37 | |02:37 | ||
− | |Next, we will learn how to use''' require() | + | |Next, we will learn how to use''' require()''' method and''' use()''' method in a '''Perl''' program. |
|- | |- | ||
Line 140: | Line 140: | ||
|- | |- | ||
| 03:03 | | 03:03 | ||
− | | '''require() | + | | '''require()''' method is used for both Perl programs and '''modules'''. |
|- | |- | ||
|03:08 | |03:08 | ||
− | | It is verified at the '''run | + | | It is verified at the '''run''' time. |
|- | |- | ||
|03:10 | |03:10 | ||
− | | One needs to give the file extension. | + | | One needs to give the '''file extension'''. |
|- | |- | ||
|03:14 | |03:14 | ||
− | | The syntax of '''use | + | | The syntax of '''use''' method is: '''use module name semicolon'''. |
|- | |- | ||
Line 164: | Line 164: | ||
|- | |- | ||
|03:30 | |03:30 | ||
− | | These are similar to '''libraries''' in other languages. | + | | These are similar to '''libraries''' in other (computer) languages. |
|- | |- | ||
Line 188: | Line 188: | ||
|- | |- | ||
|04:05 | |04:05 | ||
− | |We will write another '''Perl''' | + | |We will write another '''Perl script''' where we’ll use this''' sum dot pm''' file. |
|- | |- | ||
Line 463: | Line 463: | ||
|- | |- | ||
|10:24 | |10:24 | ||
− | |Note: "use" '''module''' is recommended over "require" module, because it determines | + | |Note: "use" '''module''' is recommended over "require" module, because it determines module availability at '''compile time'''. |
|- | |- | ||
|10:33 | |10:33 |
Revision as of 18:08, 17 February 2016
Time | Narration |
00:01 | Welcome to the Spoken Tutorial on Including files or modules in a PERL program. |
00:08 | In this tutorial, we will learn to use:
methods in PERL programming. |
00:16 | For this tutorial, I am using:
|
00:28 | You can use any text editor of your choice. |
00:32 | To follow this tutorial, you should have working knowledge of Perl programming. |
00:37 | If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. |
00:44 | 'do()' method: These are simple ways to include the source code from other files into the current script file. |
00:53 | Let us understand how to use do() method. |
00:57 | Open a new file in your text editor and name it as datetime dot pl. |
01:03 | In the datetime dot pl file, type the following code as displayed on the screen. |
01:09 | From here onwards, remember to press the Enter key after every command on the terminal. |
01:15 | Let us understand the code now. |
01:18 | The current date and time are stored in a variable dollar datestring. |
01:23 | Here, I have a function by name "msgThanks" which returns a “Thank you” message. |
01:31 | Now, press Ctrl+S to save the file. |
01:35 | Next, let us look at another Perl program which will make use of this file datetime dot pl. |
01:43 | Open a new file in your text editor and name it as main dot pl. |
01:49 | In the main dot pl file, type the following code as displayed on the screen. |
01:55 | Let me explain the code now. |
01:58 | Here, the first line prints the welcome message. |
02:03 | 'do()' method is called with the filename from where we want to use the code. |
02:09 | Current date and time are stored in the $datestring variable of datetime dot pl file. |
02:16 | And at the end, we call the msgThanks() function from the same file. |
02:21 | Now, press Ctrl+S to save the file. |
02:25 | Let us execute the program. |
02:27 | Switch back to the terminal and type: perl main dot pl and press Enter. |
02:34 | Observe the output on the terminal. |
02:37 | Next, we will learn how to use require() method and use() method in a Perl program. |
02:44 | These methods are used - when we have collections of subroutines that can be used in multiple Perl programs. |
02:52 | use() method is used only for the modules. |
02:56 | It is verified at the time of compilation. |
02:59 | There is no need to give the file extension. |
03:03 | require() method is used for both Perl programs and modules. |
03:08 | It is verified at the run time. |
03:10 | One needs to give the file extension. |
03:14 | The syntax of use method is: use module name semicolon. |
03:20 | Perl modules are the files which end with '.pm' extension. |
03:25 | Re-usability of code is implemented through modules. |
03:30 | These are similar to libraries in other (computer) languages. |
03:35 | Now, I will show a simple program with the use method to include a module in Perl code. |
03:43 | Open a new file in your text editor and name it as sum dot pm. |
03:49 | In the sum dot pm file, type the following code as displayed on the screen. |
03:55 | Here, I have a simple function which will calculate the sum of a given set of numbers. |
04:01 | Now, press Ctrl+S to save the file. |
04:05 | We will write another Perl script where we’ll use this sum dot pm file. |
04:11 | Let me open the sample program app dot pl file which I have saved already. |
04:17 | In the app dot pl file, type the following code as displayed on the screen. |
04:22 | Let me explain the code now. |
04:25 | The first line shows the use method with the module name. |
04:29 | In our case, the module name is 'sum'. |
04:33 | We are passing 1, 7, 5, 4,9 as input parameters to the function total() in sum dot pm file. |
04:44 | Again, in the next line, we are passing 1 to 10 as input parameters to the same function. |
04:52 | Now, press Ctrl+S to save the file. |
04:56 | Let us execute the program. |
04:59 | Switch back to the terminal and type: perl app dot pl and press Enter. |
05:06 | Observe the output displayed on the terminal. |
05:10 | Let us see few more options in use method. Switch back to sum dot pm in the text editor. |
05:18 | At the beginning of the source code, add the lines "use strict" semicolon, "use warnings" semicolon. |
05:27 | "use strict" and "use warnings" are compiler flags that instruct Perl to behave in a stricter way. |
05:35 | These are used to avoid common programming mistakes. |
05:39 | use strict forces the user to declare all the variables used in the program. |
05:45 | If there are errors, use strict will abort the execution. |
05:50 | use warnings will only provide warnings but continue with the execution. |
05:56 | Assume that we forgot to declare the variable $sum as my. |
06:02 | Let us now see how the same program is executed. |
06:06 | Press Ctrl+S to save the file. |
06:09 | Switch back to the terminal and type: perl app dot pl. |
06:15 | We can see that the program is aborted without executing the result. |
06:21 | The first set of lines displayed on the terminal are error messages generated by “use strict”. |
06:29 | The last two are the abort messages. |
06:32 | So, this is how the use method options work. |
06:36 | Next, let us see a Perl program where we use the require method. |
06:41 | Let me open the sample program commonfunctions dot pl which I have already saved. |
06:48 | Type the following code as displayed on the screen in your commonfunctions dot pl file.
Let us understand the code now. |
06:57 | Here, we can see a collection of commonly used functions. |
07:01 | The first function square(), returns the square of a number. |
07:06 | The second function, square underscore root(), returns the square root of a given number. |
07:12 | Next function random underscore number(), generates a random number. |
07:18 | The last function random underscore range(), generates a random number between a lower range and upper range of numbers. |
07:26 | Note that we need the 1 semicolon at the end of the file. |
07:31 | This is because Perl needs the last expression in the file to return a true value. |
07:37 | Now, press Ctrl+S to save the file. |
07:41 | Next, we will write a Perl program in which we’ll call these subroutines using require method. |
07:48 | Let me open the sample program callprogram dot pl which I have already saved. |
07:54 | Type the following code as displayed on the screen in your file. Let me explain the code now. |
08:02 | require reads the commonfunctions dot pl file containing Perl code and compiles it. |
08:09 | This program gives 4 options to the user. The user has to choose one option at a time. |
08:17 | 1: (one) is to find the square of a number. |
08:20 | 2: Two is for square root of a number. |
08:23 | 3: Three is for a random number in the given range. 4: Four is to quit the program. |
08:29 | If option 1 (one) is typed, it will ask the user to enter a number. |
08:34 | The value is stored in $number. The value is passed to the function square() in commonfunctions dot pl file. |
08:44 | The function returns the square of a number. |
08:47 | The print statement prints the square of a number as output. |
08:52 | If option 2 (two) is typed, the square root of a number is displayed as output. |
08:58 | The execution is followed as explained in the previous function square(). |
09:03 | If option 3 (three) is typed, a random number is displayed as output in the given range. |
09:09 | Else, if option is 4 (four), the program exits. If any option other than the ones specified is given, the print statement says “Incorrect option”. |
09:20 | Note that in this program, we have called only three functions out of four from commonfunctions dot pl. |
09:28 | Now, press Ctrl+S to save the file. |
09:31 | Let us execute the program. |
09:34 | Switch back to the terminal and type: perl callprogram dot pl. |
09:41 | Observe the output. |
09:44 | I'll execute the program once again with a different option. |
09:49 | Type: perl callprogram dot pl. |
09:53 | Now, enter the option as 3. |
09:56 | Enter a lower range as 50. |
09:59 | Enter a upper range as 99. |
10:02 | We can see the random number is generated in the given range of numbers. |
10:08 | Try other options on your own. |
10:11 | This brings us to the end of this tutorial. Let us summarize. |
10:16 | In this tutorial, we learnt to use:
|
10:24 | Note: "use" module is recommended over "require" module, because it determines module availability at compile time. |
10:33 | Here is an assignment for you. Write a Perl program reminder.pl where you will write a letter to the participants. |
10:41 | Prompt the user to enter To and From name. |
10:45 | Call the subroutines from Letter dot pm using ‘use’ method. |
10:50 | Write the below functions in Letter dot pm file. |
10:54 | LetterDate() function returns the current date and time. |
10:58 | To() function returns the names of the participants. |
11:02 | From() function returns the sender's name. |
11:05 | Lettermsg() function returns the contents of the letter. |
11:09 | Thanksmsg() function returns "thanks" and "regards". |
11:13 | The output should be displayed as shown here. |
11:20 | The video at the following link summarizes the spoken tutotial project. Please download and watch it. |
11:27 | The Spoken Tutorial Project team:
|
11:36 | For more details, please write to us. |
11:40 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
More information on this mission is available at this link. |
11:51 | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |