Difference between revisions of "PERL/C3/Perl-Module-Library-(CPAN)/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
| 00:01 | | 00:01 | ||
− | |Welcome to the''' Spoken Tutorial''' on''' | + | |Welcome to the''' Spoken Tutorial''' on how to use '''Perl Module Library''' i.e. '''CPAN''' |
|- | |- | ||
| 00:08 | | 00:08 | ||
− | |In this tutorial we will learn to use existing '''modules''' and | + | |In this tutorial, we will learn to use existing '''modules''' and create new modules in''' PERL'''. |
|- | |- | ||
| 00:16 | | 00:16 | ||
− | |To record this tutorial, I am using | + | |To record this tutorial, I am using: |
− | + | '''Ubuntu Linux''' 12.04 operating system | |
− | + | '''Perl''' 5.14.2 and | |
− | + | ''''gedit' Text Editor'''. | |
|- | |- | ||
Line 33: | Line 33: | ||
|- | |- | ||
| 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. | |
|- | |- | ||
| 00:55 | | 00:55 | ||
− | |'''CPAN''' | + | |'''CPAN:'''PERL is an '''open source''' language and anyone can contribute to PERL's standard '''CPAN library'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 01:03 | | 01:03 | ||
− | | '''CPAN '''has thousands of ready-to-use modules written by different authors | + | | '''CPAN '''has thousands of ready-to-use modules written by different authors. |
|- | |- | ||
|01:09 | |01:09 | ||
− | |The official website of '''CPAN '''is www.cpan.org | + | |The official website of '''CPAN '''is: |
+ | '''www.cpan.org''' | ||
|- | |- | ||
Line 58: | Line 57: | ||
|- | |- | ||
| 01:24 | | 01:24 | ||
− | |This gives me access to the | + | |This gives me access to the '''function'''s which are already written inside this module. |
|- | |- | ||
Line 66: | Line 65: | ||
|- | |- | ||
| 01:32 | | 01:32 | ||
− | |Type''' perldoc List colon colon Util ''' | + | |Type:''' perldoc List colon colon Util'''. |
|- | |- | ||
| 01:38 | | 01:38 | ||
− | | You may get an error saying | + | | You may get an '''error''' saying '''You need to install the perl hyphen doc package to use this program'''. |
|- | |- | ||
| 01:46 | | 01:46 | ||
− | |This indicates, you need to install the '''perl hyphen doc | + | |This indicates, you need to install the '''perl hyphen doc''' package. |
|- | |- | ||
Line 86: | Line 85: | ||
|- | |- | ||
| 02:01 | | 02:01 | ||
− | |What you see here is the documentation for the''' List colon colon Util ''' module. | + | |What you see here is the documentation for the''' List colon colon Util''' module. |
|- | |- | ||
| 02:08 | | 02:08 | ||
− | |Note that the documentation contains | + | |Note that the documentation contains- |
− | + | description of the module, | |
− | + | example of how to use it | |
− | + | and an '''overview'''. | |
− | + | ||
|- | |- | ||
|02:20 | |02:20 | ||
− | |Hit the | + | |Hit the 'Q' key to exit the '''perldoc viewer'''. |
− | + | ||
|- | |- | ||
|02:25 | |02:25 | ||
− | |Next, we will see how to use''' List colon colon Util | + | |Next, we will see how to use''' List colon colon Util''' module in a '''Perl''' program. |
|- | |- | ||
Line 111: | Line 108: | ||
|- | |- | ||
| 02:40 | | 02:40 | ||
− | |In your''' exist underscore | + | |In your''' exist underscore modules dot pl''' file, type the following code as displayed on the screen. |
|- | |- | ||
Line 119: | Line 116: | ||
|- | |- | ||
| 02:50 | | 02:50 | ||
− | |'''use List colon colon Util''' tells '''Perl '''to find and load | + | |'''use List colon colon Util''' tells '''Perl''' to find and '''load''' the module '''List colon colon Util.''' |
|- | |- | ||
| 03:00 | | 03:00 | ||
− | |'''qw()''' function extracts words out of the string using a '''delimiter '''and returns the words as a list. | + | |'''qw()''' function extracts words out of the '''string''' using a '''delimiter '''and returns the words as a '''list'''. |
|- | |- | ||
Line 131: | Line 128: | ||
|- | |- | ||
| 03:13 | | 03:13 | ||
− | |While importing a '''module''', it | + | |While importing a '''module''', it '''import'''s only the '''subroutines''' specified in the '''list''' into our program. |
|- | |- | ||
Line 139: | Line 136: | ||
|- | |- | ||
| 03:26 | | 03:26 | ||
− | |The ''' | + | |The module will '''export''' its '''subroutines '''and '''variables '''into our program. |
|- | |- | ||
| 03:32 | | 03:32 | ||
|The most popular '''subroutines '''available in''' List colon colon Util''' are: | |The most popular '''subroutines '''available in''' List colon colon Util''' are: | ||
− | + | '''first''' - which returns the first '''element''' in the '''list'''. | |
− | '''first''' - which returns the first element in the list | + | |
|- | |- | ||
| 03:42 | | 03:42 | ||
− | |'''max''' –this returns the highest numerical value in the list | + | |'''max''' –this returns the highest numerical value in the list. |
|- | |- | ||
| 03:47 | | 03:47 | ||
− | |'''maxstr''' | + | |'''maxstr'''- returns the highest '''string''' in the list. |
|- | |- | ||
| 03:52 | | 03:52 | ||
− | |'''min''' this returns the lowest numerical value | + | |'''min'''- this returns the lowest numerical value. |
|- | |- | ||
| 03:57 | | 03:57 | ||
− | |'''minstr''' – returns the lowest string in the list | + | |'''minstr''' – returns the lowest string in the list. |
|- | |- | ||
| 04:02 | | 04:02 | ||
− | |'''shuffle''' – returns the values of the input in a random order | + | |'''shuffle''' – this returns the values of the '''input''' in a random order. |
|- | |- | ||
| 04:08 | | 04:08 | ||
− | |'''sum''' – returns the numerical sum of all the elements in the list | + | |'''sum''' – returns the numerical sum of all the elements in the list. |
|- | |- | ||
| 04:14 | | 04:14 | ||
− | |There is no need to write separate source code for each ''' | + | |There is no need to write separate source code for each '''function'''. |
|- | |- | ||
Line 181: | Line 177: | ||
|- | |- | ||
| 04:23 | | 04:23 | ||
− | |These are the | + | |These are the '''input'''s I am passing to the function '''max, min, sum '''and''' shuffle.''' |
|- | |- | ||
| 04:30 | | 04:30 | ||
− | |And these are the '''print '''statements. | + | |And, these are the '''print '''statements. |
|- | |- | ||
| 04:33 | | 04:33 | ||
− | |Now, press''' Ctrl+S''' to save the file. | + | |Now, press''' Ctrl+S''' to '''save''' the file. |
|- | |- | ||
| 04:37 | | 04:37 | ||
− | |Let us execute the program. | + | |Let us '''execute''' the program. |
|- | |- | ||
| 04:40 | | 04:40 | ||
− | |Switch back to the | + | |Switch back to the terminal and type: '''perl exist underscore modules dot pl''' and press''' Enter'''. |
|- | |- | ||
| 04:49 | | 04:49 | ||
− | |Observe the output. | + | |Observe the '''output'''. |
|- | |- | ||
Line 209: | Line 205: | ||
|- | |- | ||
| 04:58 | | 04:58 | ||
− | |Next we will see how to create new '''Perl | + | |Next, we will see how to create a new '''Perl module''' and add it to '''CPAN'''. |
|- | |- | ||
| 05:04 | | 05:04 | ||
− | |Below are the steps to create a | + | |Below are the steps to create a module: |
|- | |- | ||
| 05:08 | | 05:08 | ||
− | | Create a place to develop the | + | | Create a place to develop the module. |
|- | |- | ||
| 05:11 | | 05:11 | ||
− | | Create skeleton files for the | + | | Create skeleton files for the module. |
|- | |- | ||
| 05:14 | | 05:14 | ||
− | | | + | | '''Document''' the module. |
|- | |- | ||
| 05:16 | | 05:16 | ||
− | | Write the '''Perl code''' | + | | Write the '''Perl code'''. |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | Write the code for testing | + | | Write the code for testing. |
|- | |- | ||
| 05:20 | | 05:20 | ||
− | | Distribute the '''module '''in '''CPAN''' | + | | Distribute the '''module '''in '''CPAN'''. |
|- | |- | ||
| 05:24 | | 05:24 | ||
− | |'''Perl''' is distributed with a program called''' h2xs''' which is used to generate files for a new | + | |'''Perl''' is distributed with a program called''' h2xs''' which is used to generate files for a new module. |
|- | |- | ||
| 05:32 | | 05:32 | ||
− | |'''Math colon colon Simple''' specifies our | + | |'''Math colon colon Simple''' specifies our module name. |
|- | |- | ||
| 05:37 | | 05:37 | ||
− | |This is used to generate the directory | + | |This is used to generate the directory which should clearly identify the module it contains. |
|- | |- | ||
| 05:43 | | 05:43 | ||
− | |Basically, it creates skeleton files for the | + | |Basically, it creates skeleton files for the module.''' hyphen PAX''' are options that omit '''autoload''' and '''autogenerate'''. |
− | + | ||
− | ''' | + | |
|- | |- | ||
| 05:54 | | 05:54 | ||
− | |Let's create a new ''' | + | |Let's create a new module- '''Math colon colon Simple.''' |
|- | |- | ||
Line 267: | Line 261: | ||
|- | |- | ||
| 06:06 | | 06:06 | ||
− | |Let us switch to | + | |Let us switch to terminal to execute the''' h2xs''' command. |
|- | |- | ||
| 06:12 | | 06:12 | ||
− | |Type | + | |Type: '''h2xs hyphen PAXn Math colon colon Simple'''. |
|- | |- | ||
| 06:20 | | 06:20 | ||
− | |The '''h2xs | + | |The '''h2xs''' program generates all these files required to distribute the module. |
|- | |- | ||
| 06:27 | | 06:27 | ||
− | |Let us change directory to''' Math hyphen Simple ''' | + | |Let us change directory to''' Math hyphen Simple'''. |
|- | |- | ||
| 06:33 | | 06:33 | ||
− | |Note the '''directory''' | + | |Note the '''directory path''' on your machine. It may be as''' Math forward slash Simple'''. |
|- | |- | ||
| 06:41 | | 06:41 | ||
− | |Type | + | |Type "ls" to list all the files in the directory. We can see the following files. |
|- | |- | ||
| 06:49 | | 06:49 | ||
− | |The | + | |The "Changes" file is where we will keep track of changes, made to our module, when we write new versions. |
− | + | ||
− | + | ||
|- | |- | ||
| 06:58 | | 06:58 | ||
− | |'''lib subdirectory '''contains the | + | |'''lib subdirectory '''contains the module. |
|- | |- | ||
| 07:02 | | 07:02 | ||
− | |'''MANIFEST''' contains a list of files in this | + | |'''MANIFEST''' contains a list of files in this directory. |
|- | |- | ||
| 07:07 | | 07:07 | ||
− | |'''Makefile''' is a | + | |'''Makefile''' is a Perl program used to create a '''Unix Makefile'''. |
|- | |- | ||
| 07:12 | | 07:12 | ||
− | |We will use this '''Makefile''' to test and install our | + | |We will use this '''Makefile''' to test and install our module. |
|- | |- | ||
| 07:18 | | 07:18 | ||
− | |Test | + | |'''Test script'''s will be in''' 't' subdirectory.''' |
|- | |- | ||
| 07:22 | | 07:22 | ||
− | |The tests are simple '''Perl scripts''' but with a''' dot t''' | + | |The '''tests''' are simple '''Perl scripts''', but with a''' dot t extension''' used for '''unit testing'''. |
|- | |- | ||
| 07:30 | | 07:30 | ||
− | |'''Simple.pm''' is our | + | |'''Simple.pm''' is our module. |
|- | |- | ||
| 07:34 | | 07:34 | ||
− | |All these files are generated automatically when we execute the '''h2xs | + | |All these files are generated automatically when we execute the '''h2xs''' command. |
|- | |- | ||
Line 332: | Line 324: | ||
|- | |- | ||
| 07:45 | | 07:45 | ||
− | |Change the | + | |Change the directory to''' lib forward slash Math'''. |
|- | |- | ||
| 07:51 | | 07:51 | ||
− | |Now we will open the''' simple.pm''' file to see the existing content. | + | |Now, we will open the''' simple.pm''' file to see the existing content. |
|- | |- | ||
| 07:57 | | 07:57 | ||
− | |Type:''' gedit Simple.pm''' | + | |Type:''' gedit Simple.pm'''. |
|- | |- | ||
| 08:02 | | 08:02 | ||
− | |What we see here is a documented functional | + | |What we see here is a documented, functional Perl module that doesn't do anything. |
|- | |- | ||
Line 352: | Line 344: | ||
|- | |- | ||
| 08:16 | | 08:16 | ||
− | |Add the below code after the text | + | |Add the below code after the text: "Preloaded methods go here". |
|- | |- | ||
| 08:22 | | 08:22 | ||
− | |Here we will add four subroutines''' add, subtract, multiply '''and '''divide'''. | + | |Here, we will add four subroutines''' add, subtract, multiply '''and '''divide'''. |
|- | |- | ||
Line 364: | Line 356: | ||
|- | |- | ||
| 08:33 | | 08:33 | ||
− | |Now, let's create a sample Perl program to test our code that | + | |Now, let's create a sample Perl program to test our code that makes sure it is working properly. |
|- | |- | ||
Line 376: | Line 368: | ||
|- | |- | ||
| 08:55 | | 08:55 | ||
− | |Add the following code after the existing code | + | |Add the following code after the existing code: “Insert your test code below..”. |
|- | |- | ||
Line 388: | Line 380: | ||
|- | |- | ||
| 09:10 | | 09:10 | ||
− | |Let us '''run''' the test script. | + | |Let us '''run''' the '''test script'''. |
|- | |- | ||
Line 396: | Line 388: | ||
|- | |- | ||
| 09:19 | | 09:19 | ||
− | |We see this error message because the '''Perl '''script cannot find '''Simple.pm''' in its directory. | + | |We see this '''error''' message because the '''Perl '''script cannot find '''Simple.pm''' in its directory. |
|- | |- | ||
Line 412: | Line 404: | ||
|- | |- | ||
| 09:43 | | 09:43 | ||
− | | | + | |Perl modules and '''libraries '''can be loaded from these directories. |
|- | |- | ||
Line 420: | Line 412: | ||
|- | |- | ||
| 09:57 | | 09:57 | ||
− | |Alternately, we can add files to''' at the rate INC''' at | + | |Alternately, we can add files to''' at the rate INC''' at run time using '-I' option. |
|- | |- | ||
| 10:06 | | 10:06 | ||
− | |Now let us switch to the | + | |Now, let us switch to the terminal. |
|- | |- | ||
| 10:10 | | 10:10 | ||
− | |I will '''execute''' the program using the | + | |I will '''execute''' the program using the''' '-I' command line parameter'''. |
|- | |- | ||
Line 468: | Line 460: | ||
| 11:02 | | 11:02 | ||
|In this tutorial, we learnt to: | |In this tutorial, we learnt to: | ||
− | + | Use existing modules | |
− | + | Create new modules and how to use in the Perl program. | |
− | and how to use in the Perl program. | + | |
|- | |- | ||
Line 486: | Line 477: | ||
|- | |- | ||
| 11:24 | | 11:24 | ||
− | | '''Text colon colon Wrap''' module has a variable- | + | | '''Text colon colon Wrap''' module has a variable- "columns". Set the''' columns''' value to 30. |
|- | |- | ||
Line 494: | Line 485: | ||
|- | |- | ||
| 11:35 | | 11:35 | ||
− | |The video at the following link | + | |The video at the following link summarizes the '''Spoken Tutorial''' project.Please download and watch it. |
− | Please download and watch it. | + | |
|- | |- | ||
| 11:42 | | 11:42 | ||
|The''' Spoken Tutorial Project''' team: | |The''' Spoken Tutorial Project''' team: | ||
− | + | conducts workshops using spoken tutorials and gives certificates on passing online tests. | |
− | + | ||
|- | |- |
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. |