in reply to Detecting modules in use?

use symbol table, luke! print map { $_, $/ } grep { substr($_, -2) eq '::' } keys %::; this gives you all the entries in the main symbol table ending with '::'. this equates to all the loaded top level modules, along with some other perl internal namespaces.

come to think of it, you can just do the following: exists $::{'DBI::'} to check if HTTP::Request is loaded, do the following: exists $::{'HTTP::'} && exists $HTTP::{'Request::'} i think you can do this too:

$DBI_loaded = defined %DBI::; # or $HTTP_Request_Loaded = defined %HTTP::Request::;
so, to conclude here is code that loads DBI unless DBI is already loaded (or someone else has a package called DBI, or someone is screwing with the symbol table, or someone is blindly creating variables in the main namespace from CGI variables, etc. don't anyone say i didn't warn them.) unless (defined %DBI::) { use DBI; }

Replies are listed 'Best First'.
RE: Re: Detecting modules in use?
by KM (Priest) on May 29, 2000 at 23:51 UTC
    Another way, which will also list all modules which modules you use, use:

    $ perl -MDBI -e 'print qq{$_ => $INC{$_}\n} for keys %INC'; Carp.pm => /usr/lib/perl5/5.00503/Carp.pm DBI.pm => /usr/lib/perl5/site_perl/5.005/i386-linux/DBI.pm AutoLoader.pm => /usr/lib/perl5/5.00503/AutoLoader.pm strict.pm => /usr/lib/perl5/5.00503/strict.pm Exporter.pm => /usr/lib/perl5/5.00503/Exporter.pm DynaLoader.pm => /usr/lib/perl5/5.00503/i386-linux/DynaLoader.pm vars.pm => /usr/lib/perl5/5.00503/vars.pm

    So, you could do:

    sub() if exists $INC{'DBI.pm'};

    Now, for how to know if the connection is open, I would also suggest Apache::DBI, so you can have a persistent connection.

    Cheers,
    KM

RE: Re: Detecting modules in use?
by BBQ (Curate) on May 29, 2000 at 07:51 UTC
    Yes Obiwan :) !!

    But this takes care of part of my problem. It doesn't tell me if the database has been connected yet... Its a darn good start tho, considering we have standarized $dbh and $sth as our handlers. I guess I could go from there.

    Thanks for the tip!
      Don't know if this will help you, but $DBI::lasth remains undef until you touch DBI.

      /brother t0mas
      i don't know of any way to figure out programmatically through DBI whether or not any connections have been made. the only way i can think of is a policy oriented way such as you've suggested (standardizing on $dbh and $sth for handler names).

      i checked out the source of DBD::Pg, and from the looks of things, connect() doesn't keep track of connections. perhaps DBD::MySql does, i don't know.