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?
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 ...
#!/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.
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";
}
eval "require DBI";
my $dbi_ok = $@ ? 0 : 1;
if( $dbi_ok ){
# do DBI stuff here
}else{
print "NO DBI\n";
}
perlmonks.org content © perlmonks.org and davidrw, ikegami, jcpunk
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03