optional 'require'

Stephen Collyer scollyer at netspinner.co.uk
Wed Dec 21 15:59:44 GMT 2005


Dirk Koopman wrote:
> For some reason (probably due to old age and lack of brain brought on by
> excess alcohol) I am having difficulty determining, in an optional
> non-stopping way, whether DBI (and at least DBD::SQLite) is installed on
> the machine.
> 
> I have an app which traditionally has used flat files and DB_File to
> store the data that it has acquired. I want to allow seamless upgrade to
> a SQL based version. 
> 
> Therefore I want to test for the presence of DBI and at least one true
> SQL engine. However it is vital that this test is non destructive and
> the app carries on as normal if these are not available.
> 
> I have tried various styles of: eval { require DBI; }; and eval "require
> DBI"; so far without adequate success.
> 
> Can anyone help me?  
> 

Dirk

Here is a cut'n'paste of some working code that detects the
presence of modules. I think the operation should be clear
enough even with the extraneous fluff.

> sub detect_optional_modules
> {
>     ns_log("Detecting optional modules");
>     foreach my $module (@config::optional_modules)
>     {
>         my $name = $module;
>         $name =~ s[::][_]g;
>         $name = '$Config::Modules::' . $name;
> 
>         no strict 'refs';
> 
>         my $avail = 0;
>         if (! defined ${$name} || ${$name} == 0)
>         {
>             if (eval "require $module")
>             {
>                 $avail = 1;
>             }
>         }
> 

-- 
Regards

Stephen Collyer
Netspinner Ltd


More information about the london.pm mailing list