davejavu has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, looking for a little guidence here. I'm guessing what I'm trying to do is very simple but I can't seem to find the syntax. My situation is that I have one script that I plan on running on Windows and Linux. For registry access I'm have "use Win32;". When I run the script on Linux is compains that it can't find Win32.pm in @INC. No suprise since it's not there. What I'm wondering is how to do conditional load (in my case OS conditional load) of a module. Thanks for any help you can give me, Dave
  • Comment on How to do a conditional load of a module ?

Replies are listed 'Best First'.
Re: How to do a conditional load of a module ?
by japhy (Canon) on Jul 18, 2005 at 18:46 UTC
    There's a module in the standard distribution, if.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: How to do a conditional load of a module ?
by Zaxo (Archbishop) on Jul 18, 2005 at 18:48 UTC

    This ought to work, use if $^O =~ /win32/i, 'Win32'; That uses if.pm.

    After Compline,
    Zaxo

Re: How to do a conditional load of a module ?
by virtualsue (Vicar) on Jul 18, 2005 at 18:48 UTC
Re: How to do a conditional load of a module ?
by shiza (Hermit) on Jul 18, 2005 at 18:53 UTC
    a BEGIN {...} block may be of use.
Re: How to do a conditional load of a module ?
by davejavu (Initiate) on Jul 18, 2005 at 19:52 UTC
    Thank you (bowing and walking backwards), thank you (bowing and walking backwards), thank you (bowing and walking backwards). . . Exactly what I needed, I just didn't know what to search on.
Re: How to do a conditional load of a module ?
by davejavu (Initiate) on Jul 19, 2005 at 17:07 UTC
    Thanks Paul. I did try that, at least I thought I did but it was late, couldn't keep my eyes open and knew I had to get up early. Now just to doc exactly what I have so it may help someone else.
    use strict; use warnings; use English; use diagnostics -verbose; use File::Path; use Switch; use Cwd; #Load Windows specific moduldes use if ($^O eq "MSWin32"), "Win32"; use if ($^O eq "MSWin32"), "Win32API::Registry" => qw(:ALL); use if ($^O eq "MSWin32"), "Win32::Shortcut"; use if ($^O eq "MSWin32"), "Win32::TieRegistry"; if ($OSNAME eq "MSWin32") { $main::Registry->Delimiter("/"); }
    Two notes.
    I had to quote the modulde names since I'm using strict.
    I couldn't get the option 'Delimiter("/")' working on the "us if ... TieRegistry" line so I had to set the delimiter on it's own line

    Thanks everyone for your help. Hopefully this summary will help someone else.
    -Dave
Re: How to do a conditional load of a module ?
by davejavu (Initiate) on Jul 19, 2005 at 07:51 UTC
    Damn, spoke too soon. The "if use..." syntax does bypass the Windows module load when running on Unix, but now the load fails on Windows. Oy! The two loads that are causing me a problem both have arguements.
    use if ($^O eq "MSWin32"), "Win32API::Registry '(:ALL)'"; use if ($^O eq "MSWin32"), "Win32::TieRegistry ( Delimiter=>\"/\" )";
    I've tried all the syntax variations I can think of. Does anyone know what I'm missing ?
    Thanks, Dave
      Did you check the documentation of if?
      SYNOPSIS use if CONDITION, MODULE => ARGUMENTS;
      So it should be something like:
      use if $^O eq "MSWin32", Win32API::Registry => qw(:ALL);
      HTH

      Paul