Perl Modules question

  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

Hi all,

I not sure how to explain this, but im writing a perl daemon in which i utilize the Config::Inifiles module. Sadly this module isnt delivered by default and might need to be downloaded and compiled when missing. At this moment it is a "manual" task using >`Cpan -i Config::Inifiles` and is working uptil there is no cpan config available or the lack of any internet connection.

No i was wondering, is there a clean programatic approach where the script checks the availability of the required modules and if missing will compile them from the source delivered with the script?

-Rgrds, Chris

(How its done now)
Code: [ Select ]
#!/usr/bin/perl

my $cfgFile = './etc/amis.ini';
my $logFile = './etc/amis.log';
my $start;
my $stop;
my $restart;
my $status;
my $script;
my $state;
my $return;
my @structure;
my $sec;
my $min;
my $hour;
my $mday;
my $mon;
my $year;
my $line;
my $cif;
my $E_exit;
my $E_msg;
my $E_type;
my $getopt;
my $failure;


use strict;
if( ModCheck("Getopt::Long") and ModCheck("Config::IniFiles") ){
    use warnings;
    use Getopt::Long;
    #use Config::IniFiles;
    use Config::IniFiles;
}else{
    warn "Required modules not available, trying to install them on the fly\n
       if this fails try manually using 'cpan -i Config::IniFiles Getopt::Long'\n";
    $getopt = `cpan -i Getopt::Long`;
    $cif  = `cpan -i Config::IniFiles`;
    exit 1;
}

#...
#...
#...

sub ModCheck{
    my $module = $_[0];
    eval "use $module";
    if($@){
        return 0;
    }else{
        return 1;
    }
}
  1. #!/usr/bin/perl
  2. my $cfgFile = './etc/amis.ini';
  3. my $logFile = './etc/amis.log';
  4. my $start;
  5. my $stop;
  6. my $restart;
  7. my $status;
  8. my $script;
  9. my $state;
  10. my $return;
  11. my @structure;
  12. my $sec;
  13. my $min;
  14. my $hour;
  15. my $mday;
  16. my $mon;
  17. my $year;
  18. my $line;
  19. my $cif;
  20. my $E_exit;
  21. my $E_msg;
  22. my $E_type;
  23. my $getopt;
  24. my $failure;
  25. use strict;
  26. if( ModCheck("Getopt::Long") and ModCheck("Config::IniFiles") ){
  27.     use warnings;
  28.     use Getopt::Long;
  29.     #use Config::IniFiles;
  30.     use Config::IniFiles;
  31. }else{
  32.     warn "Required modules not available, trying to install them on the fly\n
  33.        if this fails try manually using 'cpan -i Config::IniFiles Getopt::Long'\n";
  34.     $getopt = `cpan -i Getopt::Long`;
  35.     $cif  = `cpan -i Config::IniFiles`;
  36.     exit 1;
  37. }
  38. #...
  39. #...
  40. #...
  41. sub ModCheck{
  42.     my $module = $_[0];
  43.     eval "use $module";
  44.     if($@){
  45.         return 0;
  46.     }else{
  47.         return 1;
  48.     }
  49. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • may
  • Proficient
  • Proficient
  • User avatar
  • Posts: 328
  • Loc: Holland [NL]

Post 3+ Months Ago

I found a solution.

You can add your own modules by doing something like this. You need to create a folder tree inside your application folder that looks like this.

/Application (main app container)
/Application/Lib/Modulename/

Inside this modulename you copy the .pm file that you are trying to load. i.e.

/Application/Lib/Modulename/Module.pm

In my case using the Config::IniFiles it will look something like this.

/Application/Lib/Config/IniFiles.pm

Next you need to make Perl add this location to the @INC global. The method i used and though to be the cleanest is

Code: [ Select ]

use lib "./lib";
  1. use lib "./lib";


After this Perl will load the modules in your own Lib tree when they are not found.

To overcome the compile errors that occur during BEGIN{} you can create an additional .pl file in which you load the modules. So the struct might look like this.

Main Application.
Code: [ Select ]
if(ModCheck("Getopt::Long") and ModCheck("Config::IniFiles") ){
    require "./lib/modules.pl";
}else{
    print "Some required modules are not present!";
}

## ModCheck subroutine ##
sub ModCheck{
    my $module = $_[0];
    eval "use $module";
    if($@){
        return 0;
    }else{
        return 1;
    }
}
  1. if(ModCheck("Getopt::Long") and ModCheck("Config::IniFiles") ){
  2.     require "./lib/modules.pl";
  3. }else{
  4.     print "Some required modules are not present!";
  5. }
  6. ## ModCheck subroutine ##
  7. sub ModCheck{
  8.     my $module = $_[0];
  9.     eval "use $module";
  10.     if($@){
  11.         return 0;
  12.     }else{
  13.         return 1;
  14.     }
  15. }


modules.pl
Code: [ Select ]
use Getopt::Long;
use Config::IniFiles;
return 1;
  1. use Getopt::Long;
  2. use Config::IniFiles;
  3. return 1;


well i hope this will help anyone else :)

-Rgrds Chris

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 115 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.