edw has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I wrote this piece of code to manage the modules dependencies. It works fine but I've a problem using the methods. Run the script I've this error:
"Undefined subroutine &main::gettimeofday called at..."
How can I fix it? How can I specify a module qw()? ie.
eval "require Time::HiRes qw(gettimeofday tv_interval)"
? Thank you very much for your time.

-------------------------------------------------------------------

#!/usr/bin/perl -w use strict; use Getopt::Std; use POSIX qw(strftime); my @perl_modules = ( "Encode", "HTML::SimpleLinkExtor", "HTTP::Cookies", "HTTP::Request::Common", "HTTP::Response", "LWP::UserAgent", "Net::Abuse::Utils", "Time::HiRes", ); foreach my $module (@perl_modules){ eval "require $module"; if ($@){ print " [!] Error : $module perl module seems is missing. Do y +ou want install it now via CPAN? (y/n) "; my $answer = <STDIN>; chomp $answer; if ($answer =~ m/y/i) { require CPAN; CPAN::install $module; } else { die " [!] Error : I can't install $module. It's required!\ +n"; } } else { if ($module =~ m/HTTP::Request::Common/){ eval "require HTTP::Request::Common qw(GET)"; } if ($module =~ m/Net::Abuse::Utils/){ eval 'Net::Abuse::Utils qw(:all)'; } if ($module =~ m/Time::HiRes/){ eval 'Time::HiRes qw(gettimeofday tv_interval)'; } } } .... .... .... my $start_time = gettimeofday(); .... .... ....

Replies are listed 'Best First'.
Re: Manage the modules dependencies
by moritz (Cardinal) on Jun 18, 2008 at 20:09 UTC
    Either use use, or call Package::Name->import(qw(import list));.
      If I use :
      
      if ($module =~ m/Time::HiRes/){
         eval "Time::HiRes->import(qw(gettimeofday tv_interval))";
      }
      
      It doesn't works again. I've the error message:
      
      ! Error : Time::HiRes qw(gettimeofday tv_interval) perl module seems is missing.
      
        Sorry, I wasn't very clear: first use require, then import.
Re: Manage the modules dependencies
by dragonchild (Archbishop) on Jun 18, 2008 at 20:56 UTC
    Unless this code is run as root / superuser, it's very likely that CPAN won't be able to install the modules. If the code is running as root, why aren't the modules already installed?

    It's much better to use the distribution packaging that CPAN modules come with. Take a look at Perl::Install for a distribution that has no actual modules, just scripts.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?