PERL/C3/Perl-Module-Library-(CPAN)/English

From Script | Spoken-Tutorial
Revision as of 16:37, 18 December 2015 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

>>Title of script: How to use Perl Module Library (CPAN)

Author: Nirmala Venkat

Keywords: Perl Module Library (CPAN), modules, PERL, PERL programming, video tutorial

Visual Cue Narration
Slide 1: Welcome to the Spoken Tutorial on How to use Perl Module Library (CPAN).
Slide 2:

Learning objectives

In this tutorial we will learn to
  • Use existing modules and
  • Create new modules

in PERL.

Slide 3:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Perl 5.14.2 and
  • gedit Text Editor

You can use any text editor of your choice.

Slide 4:

Pre-requisites

To follow this tutorial, you should have working knowledge of Perl programming.

If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website.

Slide 5a:

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
Slide 5b:

CPAN (Comprehensive Perl Archive Network)

CPAN
  • PERL is an open source language and anyone can contribute to PERL's standard CPAN library
  • CPAN has thousands of ready-to-use modules written by different authors

The official website of CPAN is www.cpan.org

Slide 6:

Modules : Example

We will take List::Util as an example and see how to use it.

This gives me access to the functions which are already written inside this module.

Switch to the terminal Switch to the terminal.
Type perldoc List::Util
You may get an error saying ‘You need to install the perl hyphen doc package to use this program
Slide 7:

perl-doc Installation:

This indicates, you need to install the perl hyphen doc package.

Do so using Synaptic Package Manager.

Please refer to the relevant Linux spoken tutorials on the spoken tutorial website.

Highlight the output What you see here is the documentation for the List::Util module.
Note that the documentation contains
  • description of the module,
  • example of how to use it
  • and an overview.
Type 'q' to quit Hit the 'Q' key to exit the perldoc viewer.
Next we will see how to use List::Util module in a Perl program.
Let me open a sample program exist underscore modules.pl which I have already saved.
In your exist underscore modules dot pl file, type the following code as displayed on the screen.

Let us understand the code now.

#!/usr/local/bin/perl

use strict;

use warnings;

use List::Util qw(first max maxstr min minstr reduce shuffle sum);

use List::Util tells Perl to find and load the module List::Util.

qw() function extracts words out of the string using a delimiter and returns the words as a list.

It is just a quick way of declaring an array.

While importing a module, it imports only the subroutines specified in the list into our program.

It contains general-utility list of subroutines.

The module will export its subroutines and variables into our program.

The most popular subroutines available in List::Util are:

first - which returns the first element in the list max – returns the highest numerical value in the list

maxstr – returns the highest string in the list

min – returns the lowest numerical value

minstr – returns the lowest string in the list

shuffle – returns the values of the input in a random order

sum – returns the numerical sum of all the elements in the list

There is no need to write separate source code for each functions.

We can just make use of these available subroutines in our program.

my $maximum = max 1..20;

my $minimum = min 3,9,12;

my $total = sum 1..100;

my $random =shuffle 0..51;

These are the inputs I am passing to the function max, min, sum and shuffle.
print "Maximum value:", $maximum , "\n";

print "Minimum value:", $minimum , "\n";

print "Sum of 1 to 100:", $total, "\n";

print "Random number:", $random, "\n";;

And these are the print statements.
Press Ctrl+S Now, press Ctrl+S to save the file.
Let us execute the program.
Switch back to the terminal and type

perl exist underscore modules dot pl and press Enter.

Highlight the output

Maximum value: 20

Minimum value: 3

Sum of 1 to 100: 5050

Random number: 41

Observe the output.

In Random number, you may get any value between 0 and 51.

Next we will see how to create new Perl modules and add it to CPAN.
Slide 8:

Steps in creating a module:

Below are the steps to create a module:
  • Create a place to develop the module
  • Create skeleton files for the module
  • Document the module
  • Write the Perl code
  • Write the code for testing
  • Distribute the module in CPAN
Perl is distributed with a program called h2xs which is used to generate files for a new module.

Math::Simple specifies our module name.

This is used to generate the directory, which should clearly identify the module it contains.

Basically, it creates skeleton files for the module.

-PAX are options that omit autoload and autogenerate.

Let's create a new module Math::Simple.

It will be provided with simple functions: add, subtract, multiply and divide.

Let us switch to terminal to execute the h2xs command.
Type,

h2xs -PAXn Math::Simple

Highlight the files

Writing Math-Simple/lib/Math/Simple.pm

Writing Math-Simple/Makefile.PL

Writing Math-Simple/README

Writing Math-Simple/t/Math-Simple.t

Writing Math-Simple/Changes

Writing Math-Simple/MANIFEST

The h2xs program generates all these files required to distribute the module.
$ cd Math-Simple Let us change directory to Math hyphen Simple (Math-Simple)

Note the directory path on your machine.

It may be as Math forward slash Simple.(Math/Simple)

$ ls

Changes lib Makefile.PL MANIFEST README t

Type ls to list all the files in the directory.

We can see the following files.

Highlight Changes The changes file is
  • where we will keep track of changes made to our module
  • when we write new versions.
Highlight lib lib subdirectory contains the module.
Highlight MANIFEST MANIFEST contains a list of files in this directory.
Highlight Makefile.PL Makefile is a Perl program used to create a Unix Makefile.

We will use this Makefile to test and install our module.

Highlight t Test scripts will be in t subdirectory.

The tests are simple Perl scripts but with a .t extension used for unit testing.

Highlight Simple.pm Simple.pm is our module.
All these files are generated automatically when we execute the h2xs command.
Let us open the simple.pm file.
Switch to Terminal

cd lib/Math

Change the directory to lib/Math
>> gedit Simple.pm Now we will open the simple.pm file to see the existing content.

Type gedit Simple.pm

Highlight the existing content What we see here is a documented functional Perl module that doesn't do anything.

We have to write the required functions in this file to make it do something.

Highlight Preloaded methods go here Add the below code after the text, Preloaded methods go here.
# Preloaded methods go here. Here we will add four subroutines add, subtract, multiply and divide.
Now, press Ctrl+S to save the file.
Now let's create a sample Perl program to test our code that make sure it is working properly.
Let us open the test file Math-Simple.t under subdirectory t.
Type gedit Math-Simple.t Type gedit Math-Simple.t
print "Addition:",add(19,23)."\n";

print "Subtraction:",subtract(100,23)."\n";

print "Multiply:",multiply(15,25)."\n";

print "Division:",divide(100,25)."\n";

Add the following code after the existing code, “Insert your test code below”,

Print statements will print the output.

Press Ctrl +S Now, press Ctrl+S to save the file.
Let us run the test script.
Type perl t/Math-simple.t Type perl Math-simple.t and press Enter.
Highlight the error We see this error message because the Perl script cannot find Simple.pm in its directory.

It should look inside the lib/ directory.

How can we rectify this error?

Let us see a few options for this.
Slide 10:

@INC

Change @INC from inside the script

syntax: use lib “path”

Example: use lib t/Math-simple.t

or

Begin {unshift (@INC , t/Math-simple.t )}

Change @INC from the command line

Example:

perl -Ilib t/Math-Simple.t

At the rate INC is a special variable that contains a list of directories.

Perl modules and libraries can be loaded from these directories.

This line of code tells the Perl program to add this directory path to its at the rate INC search directory.

Alternately, we can add files to at the rate INC at runtime using -I option.

Switch to the terminal Now let us switch to the terminal.
I will execute the program using the -I command line parameter

So, I will type

perl -Ilib t/Math-Simple.t

Highlight the output

1..1

ok 1 - use Math::Simple; Addition:42 Subtraction:77 Multiply:375 Division:4

Here is the output as expected.
We have tested the module and it is working fine.

Final step is to distribute the module.

Slide 11:

Distribute the Module Run these commands from the terminal:

perl Makefile.PL make make test make dist

The general procedure for installing module is to run these commands.

Installation involves copying files into the Perl library directory.

Most of us don't have permission to copy into this directory.

Since Math-Simple isn't a very useful module I am not demonstrating the installation part.

This brings us to the end of this tutorial. Let us summarise.
Slide 12:

summary

In this tutorial we learnt to
  • Use existing modules
  • Create new modules

and how to use in the Perl program.

Slide 13:

Assignment

Here is an assignment for you.
  1. Use the Text::Wrap module
  2. Make use of the Wrap() function which wraps the input text to form neat paragraphs.
  3. Text::Wrap module has a variable columns. Set the columns value to 30.
  4. Print the text to see the formatted output.
Slide 14:

About Spoken Tutorial project

The video at the following link summarises the Spoken Tutorial project.

Please download and watch it

Slide 15:

Spoken Tutorial workshops

The Spoken Tutorial Project Team
  • conducts workshops using spoken tutorials
  • and gives certificates on passing online tests.

For more details, please write to us.

Slide 16:

Acknowledgement

Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.

More information on this mission is available at this link.

This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

Nirmala Venkat