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...
Best,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; }
In reply to Installing CPAN Modules within BEGIN of PERL Script by GhostCode
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |