Hello Monks,

I am working to develop a software distribution that will be run by several users in my environment. In order this this application to function correctly, I must have the ability to dynamically install CPAN modules when found not to be present within the current PERL installation. I have the logic down; however, I am still having issues...

Logic:
1.) Test if module is currently installed with eval "require <module>";
2.) If module is not installed, install it...

Part two is where I am having problems. I have tried both the command line for cpan (cpan -I <module>) as well as the install function of CPAN::Shell (CPAN::Shell->install(<module>)). Unfortunately every time I run this script it tells me the latest version of the specific module is already installed; however, it most certainly is not. Take the example of Bone::Easy. When I run: CPAN::Shell->install("Bone::Easy"); or `cpan -I Bone::Easy`; I get the message that I already have the latest version. If I then, go on to attempt to use this module (using: require Bone::Easy;), I am met with this module not being installed.

Can anyone help me to better understand what may be going on here. I am certain I am missing something and would really appreciate the help.

Here is a copy of the script I am using to test this procedure...

use CPAN; use CPAN::Shell; BEGIN { my @Required_Modules = ("LWP::UserAgent","HTTP::Request::Com +mon","common::sense","Silly::Werder","Bone::Easy"); my @Installed_Modules = (); my @Needed_Modules = (); my @NotInstalled_Modules= (); my @Die_If_Missing = ("LWP::UserAgent","HTTP::Request::C +ommon","common::sense","Silly::Werder","Bone::Easy"); foreach (@Required_Modules) { #Eval require * for each module required. eval "require ($_)"; #See from output if module is not already installed - if so, t +hen add to list of modules needing install. if ($@ =~ m/^Can\'t locate/){push(@Needed_Modules,$_)} #If modules appears to already be installed, add to list of mo +dules installed. else {push(@Installed_Modules,$_)} } #For each entry on list of modules needing to be installed: foreach (@Needed_Modules) { #Attempt to install module using cpan #my $CPAN_Install = `cpan -I $_`; #print $CPAN_Install; CPAN::Shell->install($_); #Parse CPAN output my @CPAN_Error = split("\n",$CPAN_Install); #push module to list of installed modules unless there was a C +PAN error. push(@Installed_Modules,$_) unless $CPAN_Error[$#CPAN_Error] = +~ m/^>\(error\): Skipping/; #push module to list of modules note installed if there was a +CPAN error. push(@NotInstalled_Modules,$_) if $CPAN_Error[$#CPAN_Error] =~ + m/^>\(error\): Skipping/; } foreach(@Installed_Modules){print ("$_\n")} foreach(@NotInstalled_Modules){print ("$_\n")} <STDIN>; foreach(@NotInstalled_Modules) { my $Missing_Module = $_; foreach(@Die_If_Missing) { die("Could not find / instal critical module!") if $Missin +g_Module eq $_; } } <STDIN>; } PickupLine(); sub PickupLine { require Bone::Easy; print pickup; }
Best,
Bry

In reply to Installing CPAN Modules within BEGIN of PERL Script by GhostCode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.