How can I check the return value / exit status of perl -MCPAN -e? How can I check the return value / exit status of perl -MCPAN -e? bash bash

How can I check the return value / exit status of perl -MCPAN -e?


You can use the failed command to show all the modules that failed to make, test, or install in the current session. Unfortunately, failed doesn't return the number of failures, but simply prints the results to STDOUT.

There is probably a better way to do this, but filtering the output with the following hack seems to work:

perl -MCPAN -wE '    CPAN::Shell->install("Foo::Bar");    open my $buffer, ">", \my $failed or die $!;    my $stdout = select $buffer;    CPAN::Shell->failed;    select $stdout;    close $buffer;    exit 1 unless $failed =~ /Nothing failed in this session/'

(Code for redirecting STDOUT to a variable taken from simbabque's answer to another SO question)

Note that if a module is not found on CPAN, this will return zero, since it would never even get to the make phase.

You could also use the uptodate command, which returns 1 if the specified module is installed and up-to-date:

perl -MCPAN -wE '    $name = "Foo::Bar";    CPAN::Shell->install($name);    $mod = CPAN::Shell->expandany($name);    exit(! defined $mod || ! $mod->uptodate)'

This won't work, of course, if you're installing older versions of modules (and I'm not sure about development versions).


If you're willing to try a different installer, cpanm actually returns sane values: 0 for successful installs and upgrades, 1 for modules that aren't found or fail to install.


Use this form of CPAN install method

sudo perl -MCPAN -e 'CPAN::Shell->install("Devel::Cover") or exit 18'