Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

how to use different packages depending on host OS?

by smullis (Pilgrim)
on Oct 27, 2004 at 11:22 UTC ( #402954=perlquestion: print w/replies, xml ) Need Help??
smullis has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

Does anyone have any suggestions as to how I can use different modules / packages based on the OS of the machine running the script?

The following does not seem to work:

if ($^O =~ /Win/ ) { use Win32::Package } else { use Unix::Package }
I have a vague feeling that eval or AUTOLOAD may have something to do with it... but if I knew then I wouldn't be asking!

Many thanks in advance for any pointers...


Replies are listed 'Best First'.
Re: how to use different packages depending on host OS?
by Corion (Pope) on Oct 27, 2004 at 11:28 UTC

    I can think of at least three variants:

    my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; # /win/ will make Darwin (OSX) users unhappy :-) eval "use $module" or die "$module didn't return a true value"; die $@ if $@;
    or the manual way of doing what use does:
    BEGIN{ if ($^O =~ /MSWin32/) { require Win32::Foo; Win32::Foo->import(); } else { require Unix::Bar; Unix::Bar->import(); }; };

    Or, if you are not afraid to use modules conceived by Michael Schwern, and you do load modules from several places, like in a plugin setting:

    use UNIVERSAL::require; my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; $module->require; $module->import(1,2,3);

    There also is the way of completely emulating what use does:

    BEGIN { my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; my $file = $module . ".pm"; $file =~ s!::!/!g; require $file or die "$file didn't return a true value"; $module->import(); };

    So many options - the most sane ones are upper in this post though.

    Update: Added the fully-manual way of use emulation


      Many thanks Mr. Corion...


      ...and it all gets just a little bit clearer...

Re: how to use different packages depending on host OS?
by borisz (Canon) on Oct 27, 2004 at 11:31 UTC
    use require instead of use. See require.
    if ($^O =~ /Win/ ) { require Win32::Package; } else { require Unix::Package; }

      You will still need to call import for the package, which use does, but require does not.

      require General::Disclaimer;
      "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy
Re: how to use different packages depending on host OS?
by Anonymous Monk on Oct 27, 2004 at 14:14 UTC
    use if $^O =~ /Win32/, "Win32::Package"; use if $^O !~ /Win32/, "Unix::Package";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://402954]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-10-19 07:39 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (252 votes). Check out past polls.