in reply to Re: help with a Perl term
in thread help with a Perl term

If I understand you correctly, this may solve a problem I've been dealing with when I have scripts designed to work on NT and UNIX.
Would this work?
$is_NT = ($ENV{'OS'} eq 'Windows_NT'); if ($is_NT) { eval { use Win32::ODBC; } } else { eval { use DBI; } }
The problem exists because Win32 doesn't happen to be on my UNIX box. :) Is there another way to conditionally use a module?

Replies are listed 'Best First'.
RE: RE: Re: help with a Perl term
by KM (Priest) on Jun 08, 2000 at 20:19 UTC
    That snippet doesn't do anything if the use() fails. And, what if it is NT but Win32::ODBC isn't installed? What if it is a Win32 system with DBI installed? How about:

    eval { require DBI }; if ($@) { warn "DBI not here.. will keep checking..."; } eval { require Win32::ODBC }; if ($@) { warn "Oh oh"; } ... etc...
    or (ok, this is far fetched):

    my %subs = ('DBI' => \&use_dbi, 'Win32::ODBC' => \&use_odbc, 'Win32::OLE' => \&use_ole ); my @mods = ('DBI', 'Win32::ODBC', 'Win32::OLE'); for (@mods) { eval { require $_ }; if ($@) { warn "No $_ here"; }else{ $subs{$_}->(); last; } }

    Cheers,
    KM

RE: RE: Re: help with a Perl term
by BBQ (Curate) on Jun 08, 2000 at 22:57 UTC
    Shendal, this is totally off the eval topic, but why not just stick with one flavor? I've used DBI on Win32 and UNIX, with great results (even porting from one to the other was great). Is there any specific reason why you would want to use Win32::ODBC instead of DBI? You'll have to change the connect string that creates the database handler anyway.

    Just my R$0.02.

    #!/home/bbq/bin/perl
    # Trust no1!
      Using DBI was just an example. I had a script that used Win32::ODBC. I ported it over to UNIX because I wanted to add support for Oracle, not just Access databases. It was nice to add the functionality without breaking the old stuff -- or having to rewrite it.
      There are times where it would be nice to either use or not use, was my point. For example, using a package if it exists, exiting more gracefully if not. It has been an issue with some of my perl scripts (until now).
        Ah! I'm sorry. I hadn't realized it was just an example. I'm a true DBI defender/promoter, so I think you're example just hit my soft spot for DBI. :)

        #!/home/bbq/bin/perl
        # Trust no1!