Difference between revisions of "PERL/C3/Perl-Module-Library-(CPAN)/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 34: | Line 34: | ||
| 00:43 | | 00:43 | ||
|'''Modules: ''' | |'''Modules: ''' | ||
− | + | These are code files that contain common routines | |
− | + | That are written by different authors | |
− | + | And, can be used by several programs at a time. | |
|- | |- |
Latest revision as of 17:45, 5 October 2017
Time | Narration |
00:01 | Welcome to the Spoken Tutorial on how to use Perl Module Library i.e. CPAN |
00:08 | In this tutorial, we will learn to use existing modules and create new modules in PERL. |
00:16 | To record this tutorial, I am using:
Ubuntu Linux 12.04 operating system Perl 5.14.2 and 'gedit' Text Editor. |
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:43 | Modules:
These are code files that contain common routines That are written by different authors And, can be used by several programs at a time. |
00:55 | CPAN:PERL is an open source language and anyone can contribute to PERL's standard CPAN library. |
01:03 | CPAN has thousands of ready-to-use modules written by different authors. |
01:09 | The official website of CPAN is:
www.cpan.org |
01:17 | We will take List colon colon Util as an example and see how to use it. |
01:24 | This gives me access to the functions which are already written inside this module. |
01:30 | Switch to the terminal. |
01:32 | Type: perldoc List colon colon Util. |
01:38 | You may get an error saying You need to install the perl hyphen doc package to use this program. |
01:46 | This indicates, you need to install the perl hyphen doc package. |
01:50 | Do so using Synaptic Package Manager. |
01:55 | Please refer to the relevant Linux spoken tutorials on the spoken tutorial website. |
02:01 | What you see here is the documentation for the List colon colon Util module. |
02:08 | Note that the documentation contains-
description of the module, example of how to use it and an overview. |
02:20 | Hit the 'Q' key to exit the perldoc viewer. |
02:25 | Next, we will see how to use List colon colon Util module in a Perl program. |
02:33 | Let me open a sample program exist underscore modules.pl which I have already saved. |
02:40 | In your exist underscore modules dot pl file, type the following code as displayed on the screen. |
02:47 | Let us understand the code now. |
02:50 | use List colon colon Util tells Perl to find and load the module List colon colon Util. |
03:00 | qw() function extracts words out of the string using a delimiter and returns the words as a list. |
03:09 | It is just a quick way of declaring an array. |
03:13 | While importing a module, it imports only the subroutines specified in the list into our program. |
03:21 | It contains general-utility list of subroutines. |
03:26 | The module will export its subroutines and variables into our program. |
03:32 | The most popular subroutines available in List colon colon Util are:
first - which returns the first element in the list. |
03:42 | max –this returns the highest numerical value in the list. |
03:47 | maxstr- returns the highest string in the list. |
03:52 | min- this returns the lowest numerical value. |
03:57 | minstr – returns the lowest string in the list. |
04:02 | shuffle – this returns the values of the input in a random order. |
04:08 | sum – returns the numerical sum of all the elements in the list. |
04:14 | There is no need to write separate source code for each function. |
04:18 | We can just make use of these available subroutines in our program. |
04:23 | These are the inputs I am passing to the function max, min, sum and shuffle. |
04:30 | And, these are the print statements. |
04:33 | Now, press Ctrl+S to save the file. |
04:37 | Let us execute the program. |
04:40 | Switch back to the terminal and type: perl exist underscore modules dot pl and press Enter. |
04:49 | Observe the output. |
04:51 | In Random number, you may get any value between 0 and 51. |
04:58 | Next, we will see how to create a new Perl module and add it to CPAN. |
05:04 | Below are the steps to create a module: |
05:08 | Create a place to develop the module. |
05:11 | Create skeleton files for the module. |
05:14 | Document the module. |
05:16 | Write the Perl code. |
05:18 | Write the code for testing. |
05:20 | Distribute the module in CPAN. |
05:24 | Perl is distributed with a program called h2xs which is used to generate files for a new module. |
05:32 | Math colon colon Simple specifies our module name. |
05:37 | This is used to generate the directory which should clearly identify the module it contains. |
05:43 | Basically, it creates skeleton files for the module. hyphen PAX are options that omit autoload and autogenerate. |
05:54 | Let's create a new module- Math colon colon Simple. |
05:59 | It will be provided with simple functions: add, subtract, multiply and divide. |
06:06 | Let us switch to terminal to execute the h2xs command. |
06:12 | Type: h2xs hyphen PAXn Math colon colon Simple. |
06:20 | The h2xs program generates all these files required to distribute the module. |
06:27 | Let us change directory to Math hyphen Simple. |
06:33 | Note the directory path on your machine. It may be as Math forward slash Simple. |
06:41 | Type "ls" to list all the files in the directory. We can see the following files. |
06:49 | The "Changes" file is where we will keep track of changes, made to our module, when we write new versions. |
06:58 | lib subdirectory contains the module. |
07:02 | MANIFEST contains a list of files in this directory. |
07:07 | Makefile is a Perl program used to create a Unix Makefile. |
07:12 | We will use this Makefile to test and install our module. |
07:18 | Test scripts will be in 't' subdirectory. |
07:22 | The tests are simple Perl scripts, but with a dot t extension used for unit testing. |
07:30 | Simple.pm is our module. |
07:34 | All these files are generated automatically when we execute the h2xs command. |
07:41 | Let us open the simple.pm file. |
07:45 | Change the directory to lib forward slash Math. |
07:51 | Now, we will open the simple.pm file to see the existing content. |
07:57 | Type: gedit Simple.pm. |
08:02 | What we see here is a documented, functional Perl module that doesn't do anything. |
08:09 | We have to write the required functions in this file to make it do something. |
08:16 | Add the below code after the text: "Preloaded methods go here". |
08:22 | Here, we will add four subroutines add, subtract, multiply and divide. |
08:29 | Now, press Ctrl+S to save the file. |
08:33 | Now, let's create a sample Perl program to test our code that makes sure it is working properly. |
08:41 | Let us open the test file Math-Simple.t under the subdirectory 't'. |
08:49 | Type: gedit Math-Simple.t |
08:55 | Add the following code after the existing code: “Insert your test code below..”. |
09:02 | The Print statements will print the output. |
09:06 | Now, press Ctrl+S to save the file. |
09:10 | Let us run the test script. |
09:13 | Type: perl Math-simple.t and press Enter. |
09:19 | We see this error message because the Perl script cannot find Simple.pm in its directory. |
09:27 | It should look inside the lib directory. How can we rectify this error? |
09:33 | Let us see a few options for this. |
09:37 | At the rate INC is a special variable that contains a list of directories. |
09:43 | Perl modules and libraries can be loaded from these directories. |
09:48 | This line of code tells the Perl program to add this directory path to its at the rate INC search directory. |
09:57 | Alternately, we can add files to at the rate INC at run time using '-I' option. |
10:06 | Now, let us switch to the terminal. |
10:10 | I will execute the program using the '-I' command line parameter. |
10:16 | So, I will type: perl -Ilib t/Math-Simple.t |
10:24 | Here is the output as expected. |
10:27 | We have tested the module and it is working fine. |
10:31 | Final step is to distribute the module. |
10:34 | The general procedure for installing module is to run these commands. |
10:40 | Installation involves copying files into the Perl library directory. |
10:45 | Most of us don't have permission to copy into this directory. |
10:49 | Since Math-Simple isn't a very useful module, I am not demonstrating the installation part. |
10:57 | This brings us to the end of this tutorial. Let us summarize. |
11:02 | In this tutorial, we learnt to:
Use existing modules Create new modules and how to use in the Perl program. |
11:11 | Here is an assignment for you. |
11:13 | Use the Text colon colon Wrap module. |
11:17 | Make use of the Wrap() function which wraps the input text to form neat paragraphs. |
11:24 | Text colon colon Wrap module has a variable- "columns". Set the columns value to 30. |
11:31 | Print the text to see the formatted output. |
11:35 | The video at the following link summarizes the Spoken Tutorial project.Please download and watch it. |
11:42 | The Spoken Tutorial Project team:
conducts workshops using spoken tutorials and gives certificates on passing online tests. |
11:51 | For more details, please write to us. |
11:55 | Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. |
12:02 | More information on this mission is available at this link. |
12:06 | This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching. |