cpan training

49
Introduction to CPAN Living off other people’s code

Upload: pedro-figueiredo

Post on 17-May-2015

5.270 views

Category:

Technology


0 download

DESCRIPTION

A not so short introduction to using the CPAN

TRANSCRIPT

Page 1: CPAN Training

Introduction to CPANLiving off other people’s code

Page 2: CPAN Training

Nitpicking

• “Perl” is the language specification

• and the noun (“the Perl community”, “a Perl program”)

• and the distribution (“the Perl tar ball”)

• “perl” is a compiler implementing the language specification (currently, Perl 5)

• There is no “PERL”

Page 3: CPAN Training

Running Perl

• Compiler

• Tools

• Standard library

• CPAN

• and its configuration

Page 4: CPAN Training

Running PerlCompiler

• the “perl” bit

• jit compiler

• configuration assembled at build time, most (all?) of it can be overridden at runtime

• namely, the library path to use

Page 5: CPAN Training

Running PerlTools

• documentation

• header extraction

• module jump-starters

• debugger

• profiler

Page 6: CPAN Training

Running PerlStandard Library

• modules distributed with Perl

• Module::CoreList

• 602 modules as of 5.8.8

Page 7: CPAN Training

Running PerlLocal build

• Perl pre-installed everywhere

• vendor may overwrite your modifications

• you may break stuff

• always use a local package

Page 8: CPAN Training

Running PerlLocal build

• Compile:sh Configure -de -Dusethreadsmake && make test && make install(cd /usr/include && /usr/local/bin/h2ph *.h sys/*.h)

• uses /usr/local by default

• good enough most of the times

Page 9: CPAN Training

Running PerlLocal build

• Common options:

•-Dcc=gcc

• -Dprefix=/opt/builds/perl

• -Dlocincpth=”/foo/bar/include”

• -Dloclibpth=”/foo/bar/lib”

Page 10: CPAN Training

Running PerlLocal build

•Module lookup paths:lib/perl5 /5.8.8 /darwin-thread-multi /site_perl

Page 11: CPAN Training

Running PerlCPAN

• Comprehensive Perl Archive Network

• Source code repository for everything Perl

• Modules, but also...

• Perl itself

• Several tools/programs written in Perl

Page 12: CPAN Training

http://www.cpan.org/

Page 13: CPAN Training

Why CPAN• laziness

• re-inventing the wheel (not)

• code reuse

• tested and tried

• around 250 mirrors worldwide

• approximately 13000 modules

• lots of crap...

Page 14: CPAN Training

How to use CPAN

• Manually

• There’s a module for it

perl -MCPAN -e shell

• There’s a script for it

cpan

Page 15: CPAN Training

CPAN manual usage

• Search for a module

• http://search.cpan.org/

• Search for its dependencies

• http://cpandeps.cantrell.org.uk/

• Start with dependencies and install one by one

• Tedious and time-consuming (e.g., Catalyst depends on hundreds of modules)

Page 16: CPAN Training

http://search.cpan.org/

Page 17: CPAN Training

http://kobesearch.cpan.org/

Page 18: CPAN Training

http://cpandeps.cantrell.org.uk/

Page 19: CPAN Training

cpandeps: version

Page 20: CPAN Training

CPAN manual usage(continued)

• Organization:authorsmodules by-author by-category by-moduleportssrcscriptsdoc

Page 21: CPAN Training

CPAN manual usage (continued)

•Procedure:tar zxf Sample-Module-1.0.tar.gzcd Sample-Module-1.0perl Makefile.PLmakemake testmake install

Page 22: CPAN Training

The CPAN module• Lots of built-in facilities

• search

• automatic installation of dependencies

• look inside a distribution’s tar ball

• highly configurable

• You can use it in your programs (have a look at the source for cpandeps)

Page 23: CPAN Training

The CPAN module (continued)

•Usage:perl -MCPAN -e shelli Sample::Modulei /ample/ilook Sample::Moduleinstall Sample::Module

force install Sample::Module (!!!)

Page 24: CPAN Training

CPAN Configuration

• in $PERL5LIB/CPAN/Config.pm

• can be hand-edited or...

• using o conf in the CPAN shell

• it’s just a Perl module

Page 25: CPAN Training

CPAN Configuration (continued)

• Location of several utilities CPAN usesftp, gpg, gzip, lynx, make, ncftp, pager, tar, unzip

• Parameters for some of thesee.g., make_arg, ftp_passive

Page 26: CPAN Training

CPAN Configuration (continued)

• CPAN parametersauto_commit, build_dir, cpan_home, histfile, prerequisites, scan_cache

• build parametersmakepl_arg

• other

Page 27: CPAN Training

CPAN Configuration (continued)

• the urllist lists the CPAN mirrors we’ll use

• kept in a hash, %CPAN::Config

• the configuration is itself Perl code

• override system-wide parameters via MyConfig.pm

Page 28: CPAN Training

The CPAN module (continued)

•More usage:perl -MCPAN -e ‘install A::Module’o confo conf http_proxy http://proxy:port/o conf commito conf init /REGEXP/$PERL5LIB/CPAN/Config.pmfailedtest

Page 29: CPAN Training

The cpan script

• $PERL5BIN/cpancpan Some::Modulecpan .

Page 30: CPAN Training

Mirroring CPAN

• A full mirror is around 7gb

• CPAN::Mini and minicpan

• only the newest version of every module

• ~800mb

Page 31: CPAN Training

More usage

• Passing arguments to perl Makefile.PL

• Finding outdated modules

• Adding to $PERL5LIB

• Maintaining your own $PERL5LIB

• Smart urllist

Page 32: CPAN Training

Passing arguments to perl Makefile.pl• no standard

• remember Makefile.PL is a Perl program :)

• and there’s usually a README file or similar :)

• usually you’ll be able do define environment variablesCC=gcc perl Makefile.plCC=gcc cpan Some::Module

Page 33: CPAN Training

Finding outdated modules

• r command on the CPAN shell

• upgrade command to perform the actual upgrade

• take a string or a regexp as parameters

Page 34: CPAN Training

Adding to $PERL5LIB

• in your code:use lib qw( /opt/permodules /home/pfig/perllib );

• set PERL5LIB in your profile or startup scripts:export PERL5LIB=/opt/perlmodules:/home/pfig/perllib

Page 35: CPAN Training

Maintaining your own $PERL5LIB

• Override the default system installation

• How can i still enjoy the CPAN goodness?

• Don’t piss off your sysadmin

Page 36: CPAN Training

Maintaining your own $PERL5LIB

• Getting ready:mkdir -p ~/perl/bin \ ~/perl/man/man1 ~/perl/man/man3mkdir -p ~/.cpan/CPANcp $PERL5LIB/CPAN/Config.pm \ ~/.cpan/CPAN/MyConfig.pm

• Debian uses /etc/perl. There’s a special place in hell for them.

Page 37: CPAN Training

Maintaining your own $PERL5LIB

• Edit MyConfig.pm‘build_dir’ => q[/home/pfig/.cpan/build]‘cpan_home’ => q[/home/pfig/.cpan]‘histfile’ => q[/home/pfig/.cpan/histfile]‘keep_source_where’ => q[/home/pfig/.cpan/sources]‘makepl_arg’ => q[PREFIX=~/perl LIB=~/perl/lib INSTALLMAN1DIR=~/perl/man/man1 INSTALLMAN3DIR=~/perl/man/man3 INSTALLSCRIPT=~/perl/bin INSTALLBIN=~/perl/bin]‘mbuildpl_arg’ => q[--prefix ~/perl]

Page 38: CPAN Training

Maintaining your own $PERL5LIB

• Set up your environmentPERL5LIB=~/perl/libPERL5MAN=~/perl/manPATH=$PATH:~/perl/bin

export PERL5LIB PERL5MAN PATH

• Done!

• You now have all the CPAN shininess and you can get rid of use lib pragmas

Page 39: CPAN Training

Smart urllist• have a local mini mirror

• have the last url of your urllist point to it via file://

• CPAN will always download its indexes from one of the sites but it will try to get the package from your local disk.

• you get up-to-date indexes with no need to remember to refresh the mirror.

Page 40: CPAN Training

Danger, Will Robinson!

• if you run cpan without checking your path, you may be running the vendor’s cpan (because chances are /usr/bin appears in your path before /usr/local/bin)

• the same goes for perl -MCPAN

• And, if installing manually, the perl you use to run Makefile.PL

Page 41: CPAN Training

Which modules to use• CPAN testers

• http://www.cpantesters.org/

• CPANTS

• http://cpants.perl.org/

• Kwalitee

• http://cpants.perl.org/kwalitee.html

• Reviews on CPAN

• Ask around. Really.

Page 42: CPAN Training

CPAN testers

Page 43: CPAN Training

Kwalitee

Page 44: CPAN Training

Other stuff

• Bugshttp://rt.cpan.org/

• Contributing to CPANPAUSE

Page 45: CPAN Training

http://rt.cpan.org/

Page 46: CPAN Training

http://pause.cpan.org/

Page 47: CPAN Training

Resources

• http://search.cpan.org/

• http://cpan.org/misc/cpan-faq.html

• http://qa.perl.org/

• http://cpandeps.cantrell.org.uk/

• http://use.perl.org/

• http://perlmonks.org/

• perldoc

Page 48: CPAN Training

Thanks to...• In no particular order:

• David Cantrellhttp://www.cantrell.org.uk/david/tech/

• London.pm

• Pedro Melohttp://simplicidade.org/notes/

• Simon Wistowhttp://thegestalt.org/simon/

• And of course Larry Wall and the other Perl Wizards

Page 49: CPAN Training

A local shop, for local people

•http://london.pm.org/

• Free beer every month!

• London Perl Workshop (yearly, free)

• london.pm’s tech meetings (free, twice a year or thereabouts)