tags:

views:

784

answers:

6

Hi. I'm new to Object-Oriented programming and the perldoc page on CPAN.pm confuses the hell out of me. My program needs to download a couple of modules if they don't already exist. Is this basically just:

CPAN::Shell->install("Module::Name::Here");

or is there more to it? Does that download the package, unarchive it, and install it, or just one or two of those steps? If it's not all three, how do I do the other one (or two)? I would like it to make sure it doesn't try to re-install anything if the package is already there - is this the default behavior of the function or no?

And how can I tell if Perl couldn't connect to CPAN to get the package?

+2  A: 

Basically using CPAN is the following:

perl -MCPAN -e shell

if this is the first time you are running it, it will ask you a few questions and save the results in a configuration file.

then to install PGP::Sign just type:

install PGP::Sign

and you're set.

As for you last question, don't worry, it will say to you whether it can connect or not.

Keltia
I down voted this because the question wasn't about how to use cpan's shell, but how to use it programmatically via it's API. While useful information, it isn't technically a good answer.
Ape-inago
+1  A: 

I tried doing things programmatically with CPAN module in the past (relatively distant past - say 5 years ago) without much success, so I stopped trying. That means, of course, that things may have changed since then. However, the documentation on CPAN here should help. The promising looking CPAN::API::HOWTO only has two recipes, neither of them relevant to your problem. You might also investigate whether CPANPLUS is better - I use it interactively far more often than I do CPAN. Both are incredible modules though.

So, as @Keltia suggests, I do CPAN (CPANPLUS) interactively.

Jonathan Leffler
A: 

Keltia has it right. I'll add that his first instruction is done from the command prompt, usually as root, but not necessarily so. The second command is done from the CPAN prompt. You can also do it all on the command line, but I usually don't.

If you're using windows, your best bet is to use PPM, but its repositories are annoyingly out of date most times.

Joe Casadonte
I didn't downvote you, but I never use root if I can possibly help it. I own my own Perl installation; I don't use root to build modules. If I modified the system copy of Perl (something I regard as unsafe; would put me out of support w.r.t Perl), then maybe I'd have to use root. But otherwise - no!
Jonathan Leffler
With Strawberry best is to use CPAN and for ActiveState repository is updated frequently now.
Alexandr Ciornii
+1  A: 

As you can tell, most of us use only use CPAN.pm in the interactive mode, however, you're on the right track.

Things I can point out for the moment:

  • Yes, calling CPAN::Shell->install() will download, compile, test and install a package. It should also do the same for any dependencies the package has, recursively.
  • The default behaviour is to not install anything which is already installed (unless a newer version is available).
  • I'm not strictly sure how the error handling works - I'll look into it, and report back.
  • It might prompt your user, though.
Stobor
+4  A: 

the perldoc page on CPAN.pm confuses the hell out of me.

Yes, documentation of the CPAN API is still a bit lacking. It wasn't every really designed for programmatic use by others. You might have better luck with CPANPLUS, if that's available to you.

My program needs to download a couple of modules if they don't already exist. Is this basically just: CPAN::Shell->install("Module::Name::Here");

Yes, that's pretty much it for the simplest possible thing. In fact, that's pretty much all the 'cpan' command line program does when you type "cpan Module::Name::Here". However, you will need to have CPAN.pm configured in advance.

Does that download the package, unarchive it, and install it?

Yes, all three.

I would like it to make sure it doesn't try to re-install anything if the package is already there - is this the default behavior of the function or no?

Yes, the default behavior is not to install anything if the module is up to date. You can actually check that yourself with the "uptodate()" method like this:

my $mod = CPAN::Shell->expand("Module", "Module::Name::Here");
$mod->install unless $mod->uptodate;

And how can I tell if Perl couldn't connect to CPAN to get the package?

That's hard to do programmatically in a way that would be simple to explain. You either need to look at the output or else just check $mod->uptodate afterwards;

my $mod = CPAN::Shell->expand("Module", "Module::Name::Here");
if ( ! $mod->uptodate ) {
    $mod->install;
    die "Problems installing" unless $mod->uptodate;
}

Best of luck!

xdg
+6  A: 

No one else has mentioned it, but you have to load the CPAN config first:

use CPAN;

CPAN::HandleConfig->load;
CPAN::Shell::setup_output;
CPAN::Index->reload;

# now do your stuff

You can also look at the cpan(1) script that comes with CPAN.pm to see a lot of the programmer's interface in action. I also wrote on article for the latest issue of The Perl Review showing examples of the programmer's interface to CPAN.pm.

However, you might not need to do any of this. Why is your program downloading modules on its own? Are you trying to create a distribution that has dependencies? There are better ways to handle that so you don't have to repeat the work that's already done in other tools. For instance, see my article Creating Perl Application Distributions. You treat your program as if it's a module and get the benefit of all the cool module tools so you don't have to reinvent something.

If you tell us more about the problem that you're actually trying to solve, we might have other good answers too. :) Good luck,

brian d foy
CPAN::Shell->install will take care of all that for you, won't it? It does it for me...
Stobor
Interesting comments - maybe why I had problems all those aeons ago.
Jonathan Leffler
Lots of things call those methods on demand. So it's a good, safe practice, but may not always be necessary. (Fixing all that and documenting it is on my "someday-maybe" list)
xdg
Most of the high-level commands, such as install() call them automatically. However, I probably am a bit too cautious because I'm usually working in an environment where I'm not using the globally installed CPAN::Config or the user's CPAN::MyConfig.
brian d foy