in reply to Re^3: DBI detector?
in thread DBI detector?

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)

Replies are listed 'Best First'.
Re^5: DBI detector?
by ikegami (Patriarch) on Apr 03, 2006 at 21:13 UTC

    Oops, I hadn't noticed you had use DBI; inside the if.

    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. As you can see in my earlier fixed code, I removed that line (apparently without realising it!).

    Here's the fix with a couple of small cleanups:

    #!/usr/bin/perl use strict; use warnings; # Always good to have. print `uname -n`; my $uname = `uname`; # Only run "uname" once. print `prtconf |grep Mem` if ($uname eq 'SunOS'); print `cat /proc/meminfo` if ($uname eq 'Linux'); my $use_dbi = eval("use DBI; 1"); # More concise than checking $@. if ($use_dbi) { print "Hurray!!\n"; } else { print "NO DBI\n"; }