PERL/C3/Downloading-CPAN-module/English

From Script | Spoken-Tutorial
Revision as of 15:09, 23 December 2015 by Nirmala Venkat (Talk | contribs)

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

>>Title of script: Downloading CPAN modules

Author: Nirmala Venkat

Keywords: Perl Module Library (CPAN), cpan modules on linux, cpan modules on windows, ppm (Perl Package Module), modules, PERL, PERL programming, video tutorial

Visual Cue Narration
Slide 1: Welcome to the Spoken Tutorial on Downloading CPAN modules.
Slide 2:

Learning objectives

In this tutorial we will learn how to download required CPAN modules in
  • Ubuntu Linux OS and
  • Windows OS
Slide 3:

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 12.04 operating system
  • Windows 7
  • 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.

First we will learn how to download CPAN modules in Ubuntu Linux OS.
Switch to the terminal Switch to the terminal.
$ sudo cpan Type sudo space cpan and press Enter.

Enter the password if required.

If cpan is not installed in your system, it will prompt you for the installation process.

Please proceed with the steps.

Your computer must be connected to the internet for the installation process.

cpan[1]> We can see the prompt changes to cpan.
Slide 5:

Example : CPAN Module

Module:

Text::CSV

Say for example, I want to extract some data from a CSV file and use in my Perl program.

For this, we will use Text:: CSV module.

Before using, we have to install the Text::CSV module
Switch to the terminal.
cpan[1]> Install Text::CSV Type Install Text::CSV and press Enter.
We can see the installation of corresponding packages of this module.

Installation will take sometime to complete depending upon your internet speed.

<<PAUSE>>

Now let us check whether the module is installed successfully or not.
Hit the 'q' key to exit cpan.
$ instmodsh Type instmodsh and press Enter.
$ l Type l to list all installed modules.
Highlight Install Text::CSV Here we can see Text::CSV, which shows that the modules is installed in our system.
Type 'q' to exit.
$ gedit candidates.csv Now I will open the candidates.csv which i have already saved.
In terminal >> type gedit candidates.csv and press Enter. Type gedit candidates.csv and press Enter.
Highlight the data Here we can see name, age, gender and email details of the candidates with a comma separator.
Point the file in gedit Now I will open csvtest.pl file in which I have written a Perl program that uses this module.

This program will extract the name field values which are stored in the csv file.

#!/usr/bin/perl

use Text::CSV;

use statement loads the Text::CSV module.
my $file = "candidates.csv";

open my $fh, "<", $file or die "$file: $!";

I have declared the candidates.csv file to a local variable dollar file.

The next statement will open the file in READ mode.

my $csv = Text::CSV->new ({sep_char=>','}); Text::CSV is the class and we can create an instance, by calling the constructor with new.

This line creates an object setting the separator character to be comma (,).

while (my $row = $csv->getline ($fh)) {

print "$row->[0]\n";

} close $fh;

Here, while loop fetches the data line by line using the getline method.

getline method returns a reference to an array.

We need to dereference it, to fetch the value.

[0] - Index of zero represents the name field in the csv file.

Print statement prints the names from the csv file.

Press Ctrl+S Now, press Ctrl+S to save the file.
Let us execute the program.
In terminal >> type perl csvtest.pl and press Enter. Switch back to the terminal and type

perl csvtest.pl and press Enter.

Highlight the output Here we can see names field as output.
Next, let us see how to download required CPAN modules in Windows OS.
Slide 6:

Perl Installation on Windows OS

When Perl is installed, a utility called PPM (Perl Package Module) get installed automatically.

To use PPM, your computer must be connected to the internet.

Slide 7:

Usage: PPM can be used to

  • search module
  • install
  • remove and
  • upgrade
This utility can be used to
  • search module
  • install
  • remove and
  • upgrade

required modules on Windows OS.

Now let us open the command window in Windows OS.
To open the command window, click on Start and type cmd and press Enter.
In the command window, type

> perl -v

Type perl hyphen v

to check whether Perl is installed on your Windows OS machine.

You will see the Perl version number that is installed on your machine.

Slide 7:

Perl Installation on Windows OS.

If Perl is not installed, refer to the Perl installation tutorial on this website.

It will tell you how to install Perl on Windows OS.

Start → Run → type in cmd At the “DOS” prompt, type

ppm install Text::CSV and press Enter.

Please note, module names are case sensitive.

We can see the installation process has begun.

Wait until the installation gets completed.

Visual Cue

In the Command window,

C:/Documents >

I have copied the candidates.csv and csvtest.pl file to the present working directory.
Let us execute the Perl program now.
In command window >> type perl csvtest.pl and press Enter. In the command window type

perl csvtest.pl

and press Enter.

Highlight the output Here is the output.
This brings us to the end of this tutorial. Let us summarise.
Slide 7:

Summary

In this tutorial we learnt about how to download required CPAN modules in
  • Linux and
  • Windows
Slide 8:

Assignment

Here is an assignment for you.
  1. Try to install Date::Calc module.
  2. Make use of the given website for module search:

http://www.cpan.org/modules/

Slide 9:

About Spoken Tutorial project

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

Please download and watch it

Slide 10:

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 11: 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