Difference between revisions of "PERL/C3/Perl-Module-Library-(CPAN)/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 6: Line 6:
 
|-
 
|-
 
| 00:01
 
| 00:01
|Welcome to the''' Spoken Tutorial''' on''' How to use Perl Module Library  ie. CPAN'''
+
|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  Create new '''modules''' in''' PERL'''.
+
|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
+
* '''Ubuntu Linux''' 12.04 operating system
* '''Perl 5.14.2 and'''
+
* '''Perl''' 5.14.2 and
* '''gedit''' Text Editor
+
* ''''gedit' Text Editor'''.
  
 
|-
 
|-
Line 33: Line 33:
 
|-
 
|-
 
| 00:43
 
| 00:43
|'''Modules '''
+
|'''Modules: '''
 
* These are code files that contain common routines  
 
* These are code files that contain common routines  
 
* That are written by different authors
 
* That are written by different authors
* And can be used by several programs at a time
+
* And, can be used by several programs at a time.
  
 
|-
 
|-
Line 42: Line 42:
 
|'''CPAN'''
 
|'''CPAN'''
  
* '''PERL''' is an open source language and anyone can contribute to '''PERL's''' standard '''CPAN library'''
+
* 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 59:
 
|-
 
|-
 
| 01:24
 
| 01:24
|This gives me access to the functions which are already written inside this module.
+
|This gives me access to the '''function'''s which are already written inside this module.
  
 
|-
 
|-
Line 66: Line 67:
 
|-
 
|-
 
| 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 need to install the perl hyphen doc package to use this program'''
+
| You may get an '''error''' saying '''You need to install the perl hyphen doc package to use this program'''.
  
 
|-
 
|-
Line 86: Line 87:
 
|-
 
|-
 
| 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,  
 
* description of the module,  
Line 98: Line 99:
 
|-
 
|-
 
|02:20
 
|02:20
|Hit the ''''Q'''' key to exit the '''perldoc viewer'''.
+
|Hit the 'Q' key to exit the '''perldoc viewer'''.
  
  
Line 111: Line 112:
 
|-
 
|-
 
| 02:40
 
| 02:40
|In your''' exist underscore''' '''modules dot pl''' file, type the following code as displayed on the screen.
+
|In your''' exist underscore modules dot pl''' file, type the following code as displayed on the screen.
  
 
|-
 
|-
Line 119: Line 120:
 
|-
 
|-
 
| 02:50
 
| 02:50
|'''use List colon colon Util''' tells '''Perl '''to find and load the '''module List colon colon Util.'''
+
|'''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 132:
 
|-
 
|-
 
| 03:13
 
| 03:13
|While importing a '''module''', it imports only the '''subroutines''' specified in the list into our program.
+
|While importing a '''module''', it '''import'''s only the '''subroutines''' specified in the '''list''' into our program.
  
 
|-
 
|-
Line 139: Line 140:
 
|-
 
|-
 
| 03:26
 
| 03:26
|The '''module '''will export its '''subroutines '''and '''variables '''into our program.
+
|The module will '''export''' its '''subroutines '''and '''variables '''into our program.
  
 
|-
 
|-
Line 145: Line 146:
 
|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''' returns the highest string in the list
+
|'''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''' – 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 '''functions'''.
+
|There is no need to write separate source code for each '''function'''.
  
 
|-
 
|-
Line 181: Line 182:
 
|-
 
|-
 
| 04:23
 
| 04:23
|These are the inputs I am passing to the function '''max, min, sum '''and''' shuffle.'''
+
|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''' terminal''' and type  '''perl exist underscore modules dot pl''' and press''' Enter'''.
+
|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 213: Line 214:
 
|-
 
|-
 
| 05:04
 
| 05:04
|Below are the steps to create a '''module''':
+
|Below are the steps to create a module:
  
 
|-
 
|-
 
| 05:08
 
| 05:08
| Create a place to develop the '''module'''
+
| Create a place to develop the module.
  
 
|-
 
|-
 
| 05:11
 
| 05:11
| Create skeleton files for the '''module'''
+
| Create skeleton files for the module.
  
 
|-
 
|-
 
| 05:14
 
| 05:14
| Document the '''module'''
+
| 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 '''module'''.
+
|'''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 '''module''' name.  
+
|'''Math colon colon Simple''' specifies our module name.  
  
 
|-
 
|-
 
| 05:37
 
| 05:37
|This is used to generate the directory, which should clearly identify the '''module''' it contains.  
+
|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 '''module'''.
+
|Basically, it creates skeleton files for the module.
 
+
''' hypen PAX''' are options that omit '''autoload''' and '''autogenerate'''.
''' hypen PAX''' are options that omit autoload and autogenerate.
+
  
 
|-
 
|-
 
| 05:54
 
| 05:54
|Let's create a new '''module Math colon colon Simple.'''
+
|Let's create a new module- '''Math colon colon Simple.'''
  
 
|-
 
|-
Line 267: Line 267:
 
|-
 
|-
 
| 06:06
 
| 06:06
|Let us switch to '''terminal''' to execute the''' h2xs''' command.
+
|Let us switch to terminal to execute the''' h2xs''' command.
  
 
|-
 
|-
 
| 06:12
 
| 06:12
|Type, '''h2xs hyphen PAXn Math colon colon Simple'''
+
|Type: '''h2xs hyphen PAXn Math colon colon Simple'''
  
 
|-
 
|-
Line 279: Line 279:
 
|-
 
|-
 
| 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''' path on your machine. It may be as''' Math forward slash Simple'''.
+
|Note the '''directory path''' on your machine. It may be as''' Math forward slash Simple'''.
  
 
|-
 
|-
 
| 06:41
 
| 06:41
|Type''' ls''' to list all the files in the '''directory.''' We can see the following files.
+
|Type "ls" to list all the files in the directory. We can see the following files.
  
 
|-
 
|-
 
| 06:49
 
| 06:49
|The''' changes''' file is  
+
|The "Changes" file is where we will keep track of changes made to our module when we write new versions.
* where we will keep track of changes made to our '''module '''
+
* when we write new versions.
+
  
 
|-
 
|-

Revision as of 13:25, 15 July 2016

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 – 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 new Perl modules 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.

hypen 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 make 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 runtime 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 summarises 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.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14