Usando el módulo CPAN.pm como Administrador

Instalar un módulo con CPAN.pm

Una forma de instalar un módulo desde CPAN es utilizar el módulo CPAN.pm. Supuesto que ese módulo está instalado en tu máquina, la orden para usarlo es:

%perl -MCPAN -e install Mi::Modulo

o bien puedes iniciar una sesión interactiva llamando a la función shell:

%perl -MCPAN -e shell

Si conoces el nombre del módulo a instalar, por ejemplo Text::Balanced, todo lo que tienes que hacer es escribir:

cpan> install Text::Balanced
y el modulo CPAN hará el resto.

Configuración de CPAN

La primera vez que se ejecuta el módulo CPAN se dispara un proceso de configuración que establece los valores por defecto que posibilitan la descarga, desempaquetado construcción y verificación de los modulos cuya instalacion has requerido.

En el siguiente ejemplo arrancamos el módulo CPAN como administradores del sistema:

# perl -MCPAN -e shell
o bien mas simple:
$ cpan

Obtendremos una respuesta similar a esta:

nereida:~/src/perl/Nmap-scanner# cpan
CPAN: File::HomeDir loaded ok (v0.69)

cpan shell -- CPAN exploration and modules installation (v1.9102)
ReadLine support enabled

El Comando h

cpan shell -- CPAN exploration and modules installation (v1.70)
ReadLine support enabled
Pedimos ayuda ...
cpan[1]> h

Display Information                                                (ver 1.9102)
 command  argument          description
 a,b,d,m  WORD or /REGEXP/  about authors, bundles, distributions, modules
 i        WORD or /REGEXP/  about any of the above
 ls       AUTHOR or GLOB    about files in the author's directory
    (with WORD being a module, bundle or author name or a distribution
    name of the form AUTHOR/DISTRIBUTION)

Download, Test, Make, Install...
 get      download                     clean    make clean
 make     make (implies get)           look     open subshell in dist directory
 test     make test (implies make)     readme   display these README files
 install  make install (implies test)  perldoc  display POD documentation

Upgrade
 r        WORDs or /REGEXP/ or NONE    report updates for some/matching/all modules
 upgrade  WORDs or /REGEXP/ or NONE    upgrade some/matching/all modules

Pragmas
 force  CMD    try hard to do command  fforce CMD    try harder
 notest CMD    skip testing

Other
 h,?           display this menu       ! perl-code   eval a perl command
 o conf [opt]  set and query options   q             quit the cpan shell
 reload cpan   load CPAN.pm again      reload index  load newer indices
 autobundle    Snapshot                recent        latest CPAN uploads
cpan[2]>

El Comando a

El comando a nos permite obtener información sobre los autores:

cpan> a /.*wall/
Author          CCWALLACE ("C. Chad Wallace" <cmdrwalrus@canada.com>)
Author          LWALL ("Larry Wall. Author of Perl. Busy man." <larry@wall.org>)
Author          PAWAL ("Patrik Wallstrom" <pawal@blipp.com>)
Author          SABREN ("Michal Wallace" <sabren@manifestation.com>)
Author          SHAWNPW ("Shawn P. Wallace" <shawn@as220.org>)
Author          THW ("Thomas Walloschke" <thw@cpan.org>)
Author          ZAPHAR ("Jeremy Wall" <Jeremy@marzhillstudios.com>)
7 items found

El Comando b

El comando b nos permite obtener información sobre los bundles. Los bundles son grupos de módulos relacionados. Los bundles son usados por CPAN para simplificar la instalación de un grupo de módulos. Una sóla b listará los bundles disponibles. Si se especifica un argumento se obtiene información sobre el bundle:

cpan> b /ssh/
Bundle id = Bundle::SSH
    CPAN_USERID  SZABGAB (Gabor Szabo <gabor@pti.co.il>)
    CPAN_VERSION 1.00
    CPAN_FILE    S/SZ/SZABGAB/Bundle-SSH-1.00.tar.gz
    MANPAGE      Bundle::SSH - A bundle to install modules to use SSH from Perl
    CONTAINS     Net::SSH Math::Pari Class::Loader Crypt::Random Digest::SHA1 \
                 Digest::HMAC Digest::BubbleBabble Digest::MD2 \
                 Convert::ASN1 Crypt::Rijndael Crypt::CBC Crypt::DES \
                 Crypt::DES_EDE3 Convert::PEM Data::Buffer Crypt::DSA \
                 Crypt::DH String::CRC32 Math::GMP Compress::Zlib \
                 Convert::ASCII::Armour Crypt::Blowfish Crypt::Primes \
                 Sort::Versions Tie::EncryptedHash Crypt::RSA \
                 Net::SSH::Perl \
    INST_FILE    /root/.cpan/Bundle/SSH.pm
    INST_VERSION 1.00

Evaluación de Expresiones

El comando ! permite la evaluación de expresiones Perl:

cpan[2]>  !print 13*56,"\n"
728
cpan[2]> !use Perl6::Say
Subroutine IO::Handle::say redefined at /usr/local/share/perl/5.8.4/Perl6/Say.pm line 26.
cpan[2]> !say "hello"
hello
cpan[2]> !print "hello"
hellocpan[2]>

El Comando r

El comando r nos permite conocer que módulos necesitan actualización:

cpan[2]> r /Net/
CPAN: Storable loaded ok (v2.15)
Going to read /root/.cpan/Metadata
  Database was generated on Thu, 08 May 2008 14:29:50 GMT
CPAN: YAML loaded ok (v0.66)
Going to read /root/.cpan/build/
............................................................................DONE
Found 88 old builds, restored the state of 41

Package namespace         installed    latest  in CPAN file
Mail::Internet                 1.76      2.03  MARKOV/MailTools-2.03.tar.gz
Net::ARP                        0.8       1.0  CRAZYDJ/Net-ARP-1.0.2.tgz
Net::Amazon                    0.40      0.49  BOUMENOT/Net-Amazon-0.49.tar.gz
Net::Cmd                       2.27      2.29  GBARR/libnet-1.22.tar.gz
Net::DNS                       0.59      0.63  OLAF/Net-DNS-0.63.tar.gz
Net::Daemon                    0.38      0.43  MNOONING/Net-Daemon/Net-Daemon-0.43.tar.gz
Net::Domain::TLD               1.65      1.67  ALEXP/Net-Domain-TLD-1.67.tar.gz
Net::Netstat::Wrapper          0.02      0.03  MCANTONI/Net-Netstat-Wrapper-0.03.tar.gz
Net::Pcap                      0.04      0.16  SAPER/Net-Pcap-0.16.tar.gz
Net::Ping                      2.31      2.35  SMPETERS/Net-Ping-2.35.tar.gz
Net::Proxy                     0.08      0.12  BOOK/Net-Proxy-0.12.tar.gz
Net::SCP                       0.07      0.08  IVAN/Net-SCP-0.08.tar.gz
Net::SFTP::Foreign             0.61      1.36  SALVA/Net-SFTP-Foreign-1.36.tar.gz
Net::SSH::Expect               1.04      1.09  BNEGRAO/Net-SSH-Expect-1.09.tar.gz
Net::SSL                       2.77      2.84  DLAND/Crypt-SSLeay-0.57.tar.gz
Net::SSLeay                    1.30      1.32  FLORA/Net-SSLeay-1.32.tar.gz
Net::Server                    0.96      0.97  RHANDOM/Net-Server-0.97.tar.gz
300 installed modules have no parseable version number

El comando upgrade

Una vez hemos visto que un módulo necesita actualización podemos actualizarlo con upgrade.

cpan[3]> upgrade Net::Server

Package namespace         installed    latest  in CPAN file
Net::Server                    0.96      0.97  RHANDOM/Net-Server-0.97.tar.gz
Running install for module 'Net::Server'
Running make for R/RH/RHANDOM/Net-Server-0.97.tar.gz
CPAN: LWP::UserAgent loaded ok (v5.810)
CPAN: Time::HiRes loaded ok (v1.86)
Fetching with LWP:
  http://www.perl.org/CPAN/authors/id/R/RH/RHANDOM/Net-Server-0.97.tar.gz
CPAN: Digest::SHA loaded ok (v5.45)
Fetching with LWP:
  http://www.perl.org/CPAN/authors/id/R/RH/RHANDOM/CHECKSUMS
Checksum for /root/.cpan/sources/authors/id/R/RH/RHANDOM/Net-Server-0.97.tar.gz ok
Scanning cache /root/.cpan/build for sizes
........................................................................----DONE
DEL(1/10): /root/.cpan/build/Encode-2.24-MsvFPh
...................................................
DEL(3/10): /root/.cpan/build/Module-Starter-1.470-278pfH
Net-Server-0.97/
Net-Server-0.97/t/
Net-Server-0.97/t/Server_PreFork.t
Net-Server-0.97/t/UDP_test.t
....................................
Net-Server-0.97/README
CPAN: File::Temp loaded ok (v0.18)

  CPAN.pm: Going to build R/RH/RHANDOM/Net-Server-0.97.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for Net::Server
...............................................................
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/Options.................ok
............................
All tests successful.
Files=12, Tests=190,  2 wallclock secs ( 1.10 cusr +  0.20 csys =  1.30 CPU)
  RHANDOM/Net-Server-0.97.tar.gz
  /usr/bin/make test -- OK
Running make install
Installing /usr/local/share/perl/5.8.8/Net/Server.pm
.....................................................
Appending installation info to /usr/lib/perl/5.8/perllocal.pod
  RHANDOM/Net-Server-0.97.tar.gz
  /usr/bin/make install  -- OK

cpan[4]>

El Comando ls

El comando ls nos permite listar los módulos desarrollados por un autor:

cpan> ls DCONWAY
Fetching with LWP:
  ftp://archive.progeny.com/CPAN/authors/id/D/CHECKSUMS
Fetching with LWP:
  ftp://archive.progeny.com/CPAN/authors/id/D/DC/CHECKSUMS
Fetching with LWP:
  ftp://archive.progeny.com/CPAN/authors/id/D/DC/DCONWAY/CHECKSUMS
    4299 2001-05-22 DCONWAY/Acme-Bleach-1.12.tar.gz
    2410 2002-05-01 DCONWAY/Acme-Don-t-1.00.tar.gz
    2659 2002-05-02 DCONWAY/Acme-Don-t-1.01.tar.gz
   11942 2001-06-02 DCONWAY/Attribute-Handlers-0.70.tar.gz
   12344 2001-09-02 DCONWAY/Attribute-Handlers-0.75.tar.gz
   12858 2001-11-14 DCONWAY/Attribute-Handlers-0.76.tar.gz
   ...................................... etc.etc.tg.gz
     347 2006-05-05 DCONWAY/Text-Balanced-1.98.meta
   29040 2006-05-05 DCONWAY/Text-Balanced-1.98.tar.gz
   18557 2003-04-02 DCONWAY/Text-Reform-1.08.tar.gz
   20493 2003-04-09 DCONWAY/Text-Reform-1.10.tar.gz
   20902 2003-05-07 DCONWAY/Text-Reform-1.11.tar.gz
    7871 1999-05-14 DCONWAY/Tie-SecureHash-1.02.tar.gz
    7994 1999-11-11 DCONWAY/Tie-SecureHash-1.03.tar.gz
     447 2005-08-03 DCONWAY/Toolkit-0.0.2.meta
    5491 2005-08-03 DCONWAY/Toolkit-0.0.2.tar.gz
También es posible usar comodines con el comando:
cpan> ls DCON*

La Clase CPAN::Shell

Los comandos que están disponibles en la interfaz de la shell son métodos del paquete CPAN::Shell. Cada vez que se introduce un comando shell la entrada es analizada por la rutina Text::ParseWords::shellwords de Text::ParseWords, la cual analiza la entrada como lo hacen la mayoría de las shells:

pp2@nereida:~/src/testing$ perl -MText::ParseWords -wde 0
main::(-e:1):   0
  DB<1> @w = shellwords('comando arg1 arg2 arg3');
  DB<2> x @w
0  'comando'
1  'arg1'
2  'arg2'
3  'arg3'
  DB<3> @v = shellwords('comando "arg1 arg2" arg3');
  DB<4> x @v
0  'comando'
1  'arg1 arg2'
2  'arg3'
La primera palabra se interpreta se interpreta como el nombre de un método a llamar mediante referenciado simbólico y las siguientes palabras como argumentos para ese método.

El Comando look

El comando look abre una shell en el directorio en el que se ha descargado la distribución especificada. Si no se ha descargado la distribución procederá a hacerlo:

nereida:~# cpan
CPAN: File::HomeDir loaded ok (v0.69)

cpan shell -- CPAN exploration and modules installation (v1.9102)
ReadLine support enabled

cpan[1]> look Net::Server
Trying to open a subshell in the build directory...
Working directory is /root/.cpan/build/Net-Server-0.97-6wI2wN
nereida:~/.cpan/build/Net-Server-0.97-6wI2wN# ls -l
total 132
drwxr-xr-x 8 root    root     4096 2008-05-09 07:11 blib
-rw-r--r-- 1 casiano casiano 21994 2007-07-25 17:17 Changes
drwxr-xr-x 2 casiano casiano  4096 2007-07-25 17:23 examples
drwxr-xr-x 3 casiano casiano  4096 2007-07-25 17:23 lib
-rw-r--r-- 1 root    root    27741 2008-05-09 07:11 Makefile
-rw-r--r-- 1 casiano casiano   612 2007-02-03 08:18 Makefile.PL
-rw-r--r-- 1 casiano casiano  2198 2007-02-03 08:05 MANIFEST
-rw-r--r-- 1 casiano casiano    97 2007-02-03 08:02 MANIFEST.SKIP
-rw-r--r-- 1 casiano casiano   418 2007-07-25 17:23 META.yml
-rw-r--r-- 1 root    root        0 2008-05-09 07:11 pm_to_blib
-rw-r--r-- 1 casiano casiano 47485 2007-02-03 08:19 README
drwxr-xr-x 2 casiano casiano  4096 2007-07-25 17:23 t
^D # retornamos a la shell de CPAN

Instalación de Versiones de Distribuciones

Para instalar una distribución con un número de versión específico hay que utilizar el identificador de distribución (Distribution id):

cpan[6]> d /Expect-Simple/
Distribution id = D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz
    CPAN_USERID  DJERIUS (Diab Jerius <djerius@cpan.org>)
    CONTAINSMODS Expect::Simple
    UPLOAD_DATE  2008-05-06
La instalación de un módulo es manejada por el método install de la clase CPAN::Module mientras que el de una distribución lo es por la clase CPAN::Distribution:

cpan[7]> install D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz
Running make for D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz
Fetching with LWP:
  http://www.perl.org/CPAN/authors/id/D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz
CPAN: Digest::SHA loaded ok (v5.45)
Checksum for /root/.cpan/sources/authors/id/D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz ok
Scanning cache /root/.cpan/build for sizes
............................................................................DONE
Expect-Simple-0.04/
Expect-Simple-0.04/Makefile.PL
Expect-Simple-0.04/MANIFEST.SKIP
Expect-Simple-0.04/README
Expect-Simple-0.04/META.yml
Expect-Simple-0.04/Changes
Expect-Simple-0.04/MANIFEST
Expect-Simple-0.04/t/
Expect-Simple-0.04/t/testprog
Expect-Simple-0.04/t/Expect-Simple.t
Expect-Simple-0.04/LICENSE
Expect-Simple-0.04/lib/
Expect-Simple-0.04/lib/Expect/
Expect-Simple-0.04/lib/Expect/Simple.pm
Expect-Simple-0.04/ChangeLog
CPAN: File::Temp loaded ok (v0.18)

  CPAN.pm: Going to build D/DJ/DJERIUS/Expect-Simple-0.04.tar.gz

Checking if your kit is complete...
Looks good
Writing Makefile for Expect::Simple
cp lib/Expect/Simple.pm blib/lib/Expect/Simple.pm
Manifying blib/man3/Expect::Simple.3pm
  DJERIUS/Expect-Simple-0.04.tar.gz
  /usr/bin/make -- OK
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" \
                "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/Expect-Simple....ok
All tests successful.
Files=1, Tests=6,  0 wallclock secs ( 0.12 cusr +  0.03 csys =  0.15 CPU)
  DJERIUS/Expect-Simple-0.04.tar.gz
  /usr/bin/make test -- OK
Running make install
Installing /usr/local/share/perl/5.8.8/Expect/Simple.pm
Installing /usr/local/man/man3/Expect::Simple.3pm
Writing /usr/local/lib/perl/5.8.8/auto/Expect/Simple/.packlist
Appending installation info to /usr/lib/perl/5.8/perllocal.pod
  DJERIUS/Expect-Simple-0.04.tar.gz
  /usr/bin/make install  -- OK

cpan[8]>



Subsecciones
Casiano Rodríguez León
2009-10-04