This module will eventually be replaced by CPANPLUS. CPANPLUS is kind
of a modern rewrite from ground up with greater extensibility and more
features but no full compatibility. If you're new to CPAN.pm, you
probably should investigate if CPANPLUS is the better choice for you.
If you're already used to CPAN.pm you're welcome to continue using it,
if you accept that its development is mostly (though not completely)
stalled.
The CPAN module is designed to automate the make and install of perl
modules and extensions. It includes some searching capabilities and
knows how to use Net::FTP or LWP (or lynx or an external ftp client)
to fetch the raw data from the net.
Modules are fetched from one or more of the mirrored CPAN
(Comprehensive Perl Archive Network) sites and unpacked in a dedicated
directory.
The CPAN module also supports the concept of named and versioned
bundles of modules. Bundles simplify the handling of sets of
related modules. See Bundles below.
The package contains a session manager and a cache manager. There is
no status retained between sessions. The session manager keeps track
of what has been fetched, built and installed in the current
session. The cache manager keeps track of the disk space occupied by
the make processes and deletes excess space according to a simple FIFO
mechanism.
For extended searching capabilities there's a plugin for CPAN available,
CPAN::WAIT. CPAN::WAIT is a full-text search engine
that indexes all documents available in CPAN authors directories. If
CPAN::WAIT is installed on your system, the interactive shell of
CPAN.pm will enable the wq, wr, wd, wl, and wh commands
which send queries to the WAIT server that has been configured for your
installation.
All other methods provided are accessible in a programmer style and in an
interactive shell style.
which puts you into a readline interface. You will have the most fun if
you install Term::ReadKey and Term::ReadLine to enjoy both history and
command completion.
Once you are on the command line, type 'h' and the rest should be
self-explanatory.
The function call shell takes two optional arguments, one is the
prompt, the second is the default initial command line (the latter
only works if a real ReadLine interface module is installed).
There are corresponding one-letter commands a, b, d, and m
for each of the four categories and another, i for any of the
mentioned four. Each of the four entities is implemented as a class
with slightly differing methods for displaying an object.
Arguments you pass to these commands are either strings exactly matching
the identification string of an object or regular expressions that are
then matched case-insensitively against various attributes of the
objects. The parser recognizes a regular expression only if you
enclose it between two slashes.
The principle is that the number of found objects influences how an
item is displayed. If the search finds one item, the result is
displayed with the rather verbose method as_string, but if we find
more than one, we display each object with the terse method
<as_glimpse>.
These commands take any number of arguments and investigate what is
necessary to perform the action. If the argument is a distribution
file name (recognized by embedded slashes), it is processed. If it is
a module, CPAN determines the distribution file in which this module
is included and processes that, following any dependencies named in
the module's Makefile.PL (this behavior is controlled by
prerequisites_policy.)
CPAN checks if an install is actually needed for it and prints
module up to date in the case that the distribution file containing
the module doesn't need to be updated.
CPAN also keeps track of what it has done within the current session
and doesn't try to build a package a second time regardless if it
succeeded or not. The force command takes as a first argument the
method to invoke (currently: make, test, or install) and executes the
command from scratch.
Example:
cpan> install OpenGL
OpenGL is up to date.
cpan> force install OpenGL
Running make
OpenGL-0.4/
OpenGL-0.4/COPYRIGHT
[...]
get downloads a distribution file without further action. readme
displays the README file of the associated distribution. Look gets
and untars (if not yet done) the distribution file, changes to the
appropriate directory and opens a subshell process in that directory.
ls lists all distribution files in and below an author's CPAN
directory. Only those files that contain modules are listed and if
there is more than one for any given module, only the most recent one
is listed.
CPAN.pm installs signal handlers for SIGINT and SIGTERM. While you are
in the cpan-shell it is intended that you can press ^C anytime and
return to the cpan-shell prompt. A SIGTERM will cause the cpan-shell
to clean up and leave the shell loop. You can emulate the effect of a
SIGTERM by sending two consecutive SIGINTs, which usually means by
pressing ^C twice.
CPAN.pm ignores a SIGPIPE. If the user sets inactivity_timeout, a
SIGALRM is used during the run of the perl Makefile.PL subprocess.
The commands that are available in the shell interface are methods in
the package CPAN::Shell. If you enter the shell command, all your
input is split by the Text::ParseWords::shellwords() routine which
acts like most shells do. The first word is being interpreted as the
method to be called and the rest of the words are treated as arguments
to this method. Continuation lines are supported if a line ends with a
literal backslash.
autobundle writes a bundle file into the
$CPAN::Config->{cpan_home}/Bundle directory. The file contains
a list of all modules that are both available from CPAN and currently
installed within @INC. The name of the bundle file is based on the
current date and a counter.
recompile() is a very special command in that it takes no argument and
runs the make/test/install cycle with brute force over all installed
dynamically loadable extensions (aka XS modules) with 'force' in
effect. The primary purpose of this command is to finish a network
installation. Imagine, you have a common source tree for two different
architectures. You decide to do a completely independent fresh
installation. You start on one architecture with the help of a Bundle
file produced earlier. CPAN installs the whole Bundle for you, but
when you try to repeat the job on the second architecture, CPAN
responds with a "Foo up to date" message for all modules. So you
invoke CPAN's recompile on the second architecture and you're done.
Another popular use for recompile is to act as a rescue in case your
perl breaks binary compatibility. If one of the modules that CPAN uses
is in turn depending on binary compatibility (so you cannot run CPAN
commands), then you should try the CPAN::Nox module for recovery.
Although it may be considered internal, the class hierarchy does matter
for both users and programmer. CPAN.pm deals with above mentioned four
classes, and all those classes share a set of methods. A classical
single polymorphism is in effect. A metaclass object registers all
objects of all kinds and indexes them with a string. The strings
referencing objects have a separated namespace (well, not completely
separated):
Namespace Class
words containing a "/" (slash) Distribution
words starting with Bundle:: Bundle
everything else Module or Author
Modules know their associated Distribution objects. They always refer
to the most recent official release. Developers may mark their releases
as unstable development versions (by inserting an underbar into the
module version number which will also be reflected in the distribution
name when you run 'make dist'), so the really hottest and newest
distribution is not always the default. If a module Foo circulates
on CPAN in both version 1.23 and 1.23_90, CPAN.pm offers a convenient
way to install version 1.23 by saying
install Foo
This would install the complete distribution file (say
BAR/Foo-1.23.tar.gz) with all accompanying material. But if you would
like to install version 1.23_90, you need to know where the
distribution file resides on CPAN relative to the authors/id/
directory. If the author is BAR, this might be BAR/Foo-1.23_90.tar.gz;
so you would have to say
install BAR/Foo-1.23_90.tar.gz
The first example will be driven by an object of the class
CPAN::Module, the second by an object of class CPAN::Distribution.
If you do not enter the shell, the available shell commands are both
available as methods (CPAN::Shell->install(...)) and as
functions in the calling package (install(...)).
There's currently only one class that has a stable interface -
CPAN::Shell. All commands that are available in the CPAN shell are
methods of the class CPAN::Shell. Each of the commands that produce
listings of modules (r, autobundle, u) also return a list of
the IDs of all modules within the list.
The IDs of all objects available within a program are strings that can
be expanded to the corresponding real objects with the
CPAN::Shell->expand("Module",@things) method. Expand returns a
list of CPAN::Module objects according to the @things arguments
given. In scalar context it only returns the first element of the
list.
Like expand, but returns objects of the appropriate type, i.e.
CPAN::Bundle objects for bundles, CPAN::Module objects for modules and
CPAN::Distribution objects fro distributions.
This enables the programmer to do operations that combine
functionalities that are available in the shell.
# install everything that is outdated on my disk:
perl -MCPAN -e 'CPAN::Shell->install(CPAN::Shell->r)'
# install my favorite programs if necessary:
for $mod (qw(Net::FTP Digest::MD5 Data::Dumper)){
my $obj = CPAN::Shell->expand('Module',$mod);
$obj->install;
}
# list all modules on my disk that have no VERSION number
for $mod (CPAN::Shell->expand("Module","/./")){
next unless $mod->inst_file;
# MakeMaker convention for undefined $VERSION:
next unless $mod->inst_version eq "undef";
print "No VERSION in ", $mod->id, "\n";
}
# find out which distribution on CPAN contains a module:
print CPAN::Shell->expand("Module","Apache::Constants")->cpan_file
Or if you want to write a cronjob to watch The CPAN, you could list
all modules that need updating. First a quick and dirty way:
perl -e 'use CPAN; CPAN::Shell->r;'
If you don't want to get any output in the case that all modules are
up to date, you can parse the output of above command for the regular
expression //modules are up to date// and decide to mail the output
only if it doesn't match. Ick?
If you prefer to do it more in a programmer style in one single
process, maybe something like this suits you better:
# list all modules on my disk that have newer versions on CPAN
for $mod (CPAN::Shell->expand("Module","/./")){
next unless $mod->inst_file;
next if $mod->uptodate;
printf "Module %s is installed as %s, could be updated to %s from CPAN\n",
$mod->id, $mod->inst_version, $mod->cpan_version;
}
If that gives you too much output every day, you maybe only want to
watch for three modules. You can write
for $mod (CPAN::Shell->expand("Module","/Apache|LWP|CGI/")){
as the first line instead. Or you can combine some of the above
tricks:
# watch only for a new mod_perl module
$mod = CPAN::Shell->expand("Module","mod_perl");
exit if $mod->uptodate;
# new mod_perl arrived, let me know all update recommendations
CPAN::Shell->r;
The programming interface for the classes CPAN::Module,
CPAN::Distribution, CPAN::Bundle, and CPAN::Author is still considered
beta and partially even alpha. In the following paragraphs only those
methods are documented that have proven useful over a longer time and
thus are unlikely to change.
Forces CPAN to perform a task that normally would have failed. Force
takes as arguments a method name to be called and any number of
additional arguments that should be passed to the called method. The
internals of the object get the needed changes so that CPAN.pm does
not refuse to take the action. The force is passed recursively to
all contained objects.
Returns the highest installed version of the bundle in either @INC or
$CPAN::Config-{cpan_home}>. Note that this is different from
CPAN::Module::inst_file.
Returns a list of IDs of modules contained in a distribution file.
Only works for distributions listed in the 02packages.details.txt.gz
file. This typically means that only the most recent version of a
distribution is covered.
Returns the directory into which this distribution has been unpacked.
CPAN::Distribution::force($method,@args)
Forces CPAN to perform a task that normally would have failed. Force
takes as arguments a method name to be called and any number of
additional arguments that should be passed to the called method. The
internals of the object get the needed changes so that CPAN.pm does
not refuse to take the action.
CPAN::Distribution::get()
Downloads the distribution from CPAN and unpacks it. Does nothing if
the distribution has already been downloaded and unpacked within the
current session.
CPAN::Distribution::install()
Changes to the directory where the distribution has been unpacked and
runs the external command make install there. If make has not
yet been run, it will be run first. A make test will be issued in
any case and if this fails, the install will be canceled. The
cancellation can be avoided by letting force run the install for
you.
Returns 1 if this distribution file seems to be a perl distribution.
Normally this is derived from the file name only, but the index from
CPAN can contain a hint to achieve a return value of true for other
filenames too.
Changes to the directory where the distribution has been unpacked and
opens a subshell there. Exiting the subshell returns.
CPAN::Distribution::make()
First runs the get method to make sure the distribution is
downloaded and unpacked. Changes to the directory where the
distribution has been unpacked and runs the external commands perl
Makefile.PL and make there.
Returns the hash reference that has been announced by a distribution
as the PREREQ_PM hash in the Makefile.PL. Note: works only after an
attempt has been made to make the distribution. Returns undef
otherwise.
CPAN::Distribution::readme()
Downloads the README file associated with a distribution and runs it
through the pager specified in $CPAN::Config-{pager}>.
CPAN::Distribution::test()
Changes to the directory where the distribution has been unpacked and
runs make test there.
CPAN::Distribution::uptodate()
Returns 1 if all the modules contained in the distribution are
uptodate. Relies on containsmods.
CPAN::Author, CPAN::Bundle, CPAN::Module, and CPAN::Distribution
inherit this method. It prints the data structure associated with an
object. Useful for debugging. Note: the data structure is considered
internal and thus subject to change without notice.
CPAN::Module::as_glimpse()
Returns a one-line description of the module
CPAN::Module::as_string()
Returns a multi-line description of the module
CPAN::Module::clean()
Runs a clean on the distribution associated with this module.
Returns a 44 character description of this module. Only available for
modules listed in The Module List (CPAN/modules/00modlist.long.html
or 00modlist.long.txt.gz)
CPAN::Module::force($method,@args)
Forces CPAN to perform a task that normally would have failed. Force
takes as arguments a method name to be called and any number of
additional arguments that should be passed to the called method. The
internals of the object get the needed changes so that CPAN.pm does
not refuse to take the action.
CPAN::Module::get()
Runs a get on the distribution associated with this module.
CPAN::Module::inst_file()
Returns the filename of the module found in @INC. The first file found
is reported just like perl itself stops searching @INC when it finds a
module.
CPAN::Module::inst_version()
Returns the version number of the module in readable format.
CPAN::Module::install()
Runs an install on the distribution associated with this module.
CPAN::Module::look()
Changes to the directory where the distribution associated with this
module has been unpacked and opens a subshell there. Exiting the
subshell returns.
CPAN::Module::make()
Runs a make on the distribution associated with this module.
If module is installed, peeks into the module's manpage, reads the
headline and returns it. Moreover, if the module has been downloaded
within this session, does the equivalent on the downloaded module even
if it is not installed.
CPAN::Module::readme()
Runs a readme on the distribution associated with this module.
CPAN::Module::test()
Runs a test on the distribution associated with this module.
CPAN::Module::uptodate()
Returns 1 if the module is installed and up-to-date.
Currently the cache manager only keeps track of the build directory
($CPAN::Config->{build_dir}). It is a simple FIFO mechanism that
deletes complete directories below build_dir as soon as the size of
all directories there gets bigger than $CPAN::Config->{build_cache}
(in MB). The contents of this cache may be used for later
re-installations that you intend to do manually, but will never be
trusted by CPAN itself. This is due to the fact that the user might
use these directories for building modules on different architectures.
There is another directory ($CPAN::Config->{keep_source_where}) where
the