Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

What's the connection between the CPAN programming methods to what happens in the CPAN shell?

by tphyahoo (Vicar)
on Aug 23, 2006 at 09:58 UTC ( #569046=perlquestion: print w/replies, xml ) Need Help??
tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

I'm organizing an install script that installs a bunch of CPAN mods, the easy dependencies first, then the harder, with the goal that the whole thing runs at one go and doesn't need to be rerun numerous times while CPAN sorts out the dependencies.

I would like to have something like

force install ('SQL::Translator');


notest install ('SQL::Translator') or die "died on SQL::Translator"; but that doesn't work from the programming interface, the way it does from the shell. The only method that seems to work from within the API is install ('SQL::Translator')

I suppose I could do system("perl -MCPAN -e 'force install SQL::Translator'"); over and over, or just code the whole thing up in bash, but that seems to be going against the whole spirit of why CPAN was created in the first place.

I guess the deeper issue is that I don't really understand what force/notest exactly are. They're described in the CPAN doc as pragmas, but I don't get it. They seem not to be pragmas in the sense of "strict/warning" which are just modules. That is to say, you can search for strict/warnings modules on CPAN, but not the "force" module. So I guess the meaning of pragma is different here.

What's the underlying model here? What's the connection between the CPAN api methods and commands like "force" and "install" which can be issued from the shell?

By the way, this question applies to CPANPLUS as well as CPAN. Lately I've been switching back and forth between the two, as sometimes the install is easier within one, other times the other.

2006-08-23 Retitled by holli, as per Monastery guidelines
Original title: 'force install from within a script? In what sense are force/install pragmas? (What's the connection between the CPAN programming methods to what happens in the CPAN shell?)'

  • Comment on What's the connection between the CPAN programming methods to what happens in the CPAN shell?
  • Select or Download Code

Replies are listed 'Best First'.
Re: What's the connection between the CPAN programming methods to what happens in the CPAN shell?
by xdg (Monsignor) on Aug 23, 2006 at 11:19 UTC
    What's the underlying model here?

    You have to consider the full CPAN object model. Ultimately, actions all fall to a CPAN::Distribution object and "force" is just a method that sets a flag and invokes other methods. The shell is a CPAN::Shell object that converts your commands to actions upon the appropriate objects.

    For example, installing a module means creating a module object, finding the distribution object that corresponds to it, and then installing the distribution. (Well, that's the short version anyway.)

    Here's a modified excerpt of what I use in Perl::Dist as part of building Vanilla Perl. (Warning: Perl::Dist as a whole sucks and needs to be rewritten; but using snippets is OK.)

    print "Preparing to install $name from CPAN\n"; $obj = CPAN::Shell->expandany( $name ) or die " couldn't locate $name"; if ( $obj->uptodate ) { print "$name is up to date\n"; exit } if ( $force ) { $obj->force("install"); $obj->uptodate or die "Forced installation of $name appears to have failed"; } else { $obj->install; $obj->uptodate or die "Installation of $name appears to have failed"; }


    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: force install from within a script?
by shmem (Chancellor) on Aug 23, 2006 at 10:54 UTC
    Hmm. From the CPAN manpage:
    The "force" command takes as a first argument the method to invoke (currently: "make", "test", or "install") and executes the command from scratch.
    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 does not refuse to take the action.

    So you probably have to say


    CPAN shell commands are method invocations, basically - shellish perl ;-)


    _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                  /\_/(q    /
    ----------------------------  \__(m.====.(_("always off the crowd"))."
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://569046]
Approved by shmem
[Jenda]: Sorry, 29 questions on stackoverflow regarding Perl6 this month. What a lively community.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-04-24 21:15 GMT
Find Nodes?
    Voting Booth?