DBI detector?
jcpunk
created: 2006-04-03 15:49:01
Hello monks,

I am trying to add some more helpful behavior to an existing script that is DBI dependent. On systems without DBI it complains about how DBI.pm is not found and so on.

Some of what it is supposed to do is not DBI dependend (basic stats gathering), what I want to do is run the non-DBI stuff anyway....
I thought I had it figured out, but... here is what I have tried. It still complains of no DBI module when there is none and refuses to run due to missing modules...

$use_dbi = 1;
eval("use DBI;");
$use_dbi = 0 if (not($@);
if ($use_dbi)
 {
  ...
 }
else
 {
  print "NO DBI\n";
 }
... do other stuff ...
help?

jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re: DBI detector?
created: 2006-04-03 16:01:22

Except for the extra/missing paren, some redundancy, your code you work fine. What's your exact error message? Maybe a DBI *driver* is missing, as opposed to DBI itself.

my $use_dbi = eval '
   use DBI;
   use DBD::mysql;
   1  # or undef if the above fails.
';

if ($use_dbi)
 {
  ...
 }
else
 {
  print "NO DBI\n";
 }

... do other stuff ...
Re^2: DBI detector?
created: 2006-04-03 16:18:32
Error wise I get
Can't locate DBI.pm in @INC (@INC contains: .....) at DBIDetector.pl line 14

jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re^3: DBI detector?
created: 2006-04-03 16:31:40
What's at and around line 14?
Re^4: DBI detector?
created: 2006-04-03 16:43:45
Sure... here it is
#!/usr/bin/perl
use strict;
my $use_dbi = 1;
eval("use DBI;");
$use_dbi = 0 if (not($@));

print `uname -n`;

print `prtconf |grep Mem` if (`uname` eq 'SunOS');
print `cat /proc/meminfo` if (`uname` eq 'Linux');

if ($use_dbi)
 {
  use DBI;
  print "Hurray!!\n"; 
 }
else
 {
  print "NO DBI\n";
 }
... for what it is worth...

Thanks for the interest.


jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re^5: DBI detector?
created: 2006-04-03 17:13:13

use ...; occurs at *compile* time and unconditionally. In other words, your code is identical to

#!/usr/bin/perl
use strict;
use DBI;
my $use_dbi = 1;
eval("use DBI;");
$use_dbi = 0 if (not($@));

print `uname -n`;

print `prtconf |grep Mem` if (`uname` eq 'SunOS');
print `cat /proc/meminfo` if (`uname` eq 'Linux');

if ($use_dbi)
 {
  print "Hurray!!\n"; 
 }
else
 {
  print "NO DBI\n";
 }

It's quite easy to fix: Remove the use DBI; since it's already done in the eval EXPR.

#!/usr/bin/perl

use strict;
use warnings;

print `uname -n`;

my $uname = `uname`;
print `prtconf |grep Mem` if ($uname eq 'SunOS');
print `cat /proc/meminfo` if ($uname eq 'Linux');

my $use_dbi = eval("use DBI; 1");
if ($use_dbi)
 {
  print "Hurray!!\n"; 
 }
else
 {
  print "NO DBI\n";
 }
Re: DBI detector?
created: 2006-04-03 16:41:48
first, you have to get rid of the use DBI; inside the if() statement -- that is being run at compile time (see use). For (attempting) loading at runtime, see require. Also I think your $@ logic is backwards (see [perldoc://eval])... Try something like this:
eval "require DBI";
my $dbi_ok = $@ ? 0 : 1;
if( $dbi_ok ){
  # do DBI stuff here
}else{
  print "NO DBI\n";
}
Re^2: DBI detector?
created: 2006-04-03 16:56:40
Bingo! I thank thee

jcpunk
all code is tested, and doesn't work so there :p (varient on common PM sig for my own ammusment)
Re^2: DBI detector?
created: 2006-04-03 17:19:09
Why did you switch from eval "use DBI" to eval "require DBI"? Both occur at run-time here (since the eval is executed at run-time here), but require doesn't import the symbols that use would import.
Re^3: DBI detector?
created: 2006-04-03 20:30:58
mostly cause require docs use the eval "require DBI" form as an example, and so i was pretty confident that would work w/o trying it, and the critical piece for OP was to remove the load-time 'use' call .. i also vauguely remember having problems once doing a eval "use Foo" once but really can't remember what (possibly {probably??} totally unrelated .. was a load order issue maybe??) .. so, in summary, no real good reason :)
Re^4: DBI detector?
created: 2006-04-04 01:21:17
Maybe you had problems with eval { use Foo; }. That won't work because the block of eval BLOCK is compiled when the eval is *compiled*. eval "use Foo;" is different because the expression in eval EXPR is compiled when the eval is *executed*.

perlmonks.org content © perlmonks.org and davidrw, ikegami, jcpunk

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03