Home » Perl Basics
11

Modules

Modules are Perl code that can be called from within your script to perform certain tasks.

We know that Perl is very powerful and can be use in a huge number of ways. Without modules it would be useless for everyone but the seasonal programmer with a lot of time. Modules are Perl code that can be called from within your script to perform certain tasks, i.e. send an email, connect to an FTP, etc. You do not want to have to rewrite a script to send an email or open a socket. It has been done before so no need to do it again.

Using CPAN

Cpan.org is the depository for Perl modules. It is also the interface from which you can install Perl modules.
From the prompt type “perl –MCPAN –e shell”
IMPORTANT : You need a internet access. The M option is not only for CPAN but will execute any Module whose name follows it
The CPAN shell is displayed.

  perl -MCPAN -e shell
  cpan shell -- CPAN exploration and modules installation (v1.7602)
  ReadLine support enabled
  
REMARQUE : you may see CPAN advising you to update your version. Perl comes with a basic version of CPAN and we advise you to update your CPAN right away in order to have the latest and the one with more options. The upgrade is easy .
  Cpan> install Bundle::CPAN
  
CPAN will periodically ask you for input during the install. Simply press enter each time to perform a standard install. It can be a long process as Perl will have to check for dependencies and install the one missing.
You can type help to get all the possible search options.
  Cpan> I /SSH/ 
will search for all SSH related modules. I said Modules because the cpan can contain equivalent modules from several author. Modules can also be clustered by relevance in bundles or be available separately. Be aware, however that you can find in those repository modules several years old. Be careful not to download too old of modules if you have the choice.
To get more information about the module either go on cpan online, search for it and click on it. Most of the time you will find here how to use the module within your script.
The search will return these results:

  cpan[3]> i /SSH/
  Bundle    Bundle::SSH            (SZABGAB/Bundle-SSH-1.00.tar.gz)
  Distribution    AURUM/Email-Auth-AddressHash-1.0.tar.gz
  Distribution    CHANSEN/Authen-Simple-SSH-0.1.tar.gz
  Distribution    DBROBINS/Net-SSH-Perl-1.30.tar.gz
  Distribution    DBROBINS/Net-SSH2-0.10.tar.gz
  Distribution    FLORA/Graph-Reader-LoadClassHierarchy-0.01.tar.gz
  Distribution    HEUEL/Statistics-GaussHelmert-0.05.tar.gz
  Distribution    HIROSE/Net-Scan-SSH-Server-SupportedAuth-0.02.tar.gz
  Distribution    IVAN/Net-SSH-0.08.tar.gz
  Distribution    MCANTONI/Net-Scan-SSH-Server-Version-0.01.tar.gz
  Distribution    MSCHILLI/Net-SSH-AuthorizedKeysFile-0.02.tar.gz
  Distribution    PEVANS/IPC-PerlSSH-0.06.tar.gz
  Distribution    RCLAMP/Filesys-Virtual-SSH-0.03.tar.gz
  Distribution    SCOTTS/Net-SSH-W32Perl-0.05.tar.gz
  Distribution    SZABGAB/Bundle-SSH-1.00.tar.gz
  Module    Apache::PassHtml       (JANPAZ/Apache-OutputChain-0.11.tar.gz)
  Module    Authen::Simple::SSH    (CHANSEN/Authen-Simple-SSH-0.1.tar.gz)
  Module    Bundle::SSH            (N/A)
  [………………………]
  Module    Perl::Critic::Policy::ClassHierarchies::ProhibitExplicitISA (THALJEF/perlcritic/Perl-Critic-1.051.tar.gz)
  Module    Perl::Critic::Policy::ClassHierarchies::ProhibitOneArgBless (THALJEF/perlcritic/Perl-Critic-1.051.tar.gz)
  Module    SVN::Notify::Mirror::SSH (JPEACOCK/SVN-Notify-Mirror-0.03603.tar.gz)
  Module    Statistics::GaussHelmert (HEUEL/Statistics-GaussHelmert-0.05.tar.gz)
  Module    Tangram::Schema::ClassHash (SAMV/Tangram-2.10.tar.gz)
  Module    Tree::Simple::Visitor::LoadClassHierarchy (STEVAN/Tree-Simple-VisitorFactory-0.10.tar.gz)
  Module  = URI::ssh               (GAAS/URI-1.35.tar.gz)
  Author          BKW ("Bernhard K. Weisshuhn" <bkw@cpan.org>)
  105 items found
  
To install it

  Cpan> install name-of::the-module 

To have more information about one of the module simply type it’s real name after the “i”

  cpan[5]> i Apache::PassHtml
  Strange distribution name [Apache::PassHtml]
  CPAN: LWP::UserAgent loaded ok (v2.033)
  CPAN: Time::HiRes loaded ok (v1.86)
  Fetching with LWP:
  http://ppm.activestate.com/CPAN/authors/id/J/JA/JANPAZ/CHECKSUMS
  Module id = Apache::PassHtml
  CPAN_USERID  JANPAZ (Jan Pazdziora <adelton@fi.muni.cz>)
  CPAN_VERSION undef
  CPAN_FILE    J/JA/JANPAZ/Apache-OutputChain-0.11.tar.gz
  UPLOAD_DATE  2002-08-28
  INST_FILE    (not installed) 

IMPORTANT :

Certain modules will be system specific. If you install a Linux version on your Windows version it will not work.
Modules usually have a manual to explain to you how to use them. However, some of the more confidential ones do not and you will be left with your knowledge of Perl to figure out how to use them.