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

Hey happy People.

First: I'm new here, I'm new in Perl and my english isn't very good.

My Problem:
I wrote one program for Windows and Unix. In Windows is everything okay. But there are some libraries which are "Windows-only" (aren't they?) e.g. Win32::NetResource.

Is there a posibility to ask for the OS an load only the libraries for Windows or Unix?

If you don't know what I mean...
if os = Windows, use Win32::NetResource
if not ... ehm... not.

Thank you for your help!

Replies are listed 'Best First'.
Re: Program for Windows and Unix
by dHarry (Abbot) on Feb 27, 2009 at 07:27 UTC

    The variable to test this is called $^O, you can test for different values. For example:

    if ($^O eq "MSWin32") { # Do Windows stuff }

    See perlvar for details.

Re: Program for Windows and Unix
by weismat (Friar) on Feb 27, 2009 at 07:28 UTC
    You may use $^O or $OSNAME to determine the operating system.
    Check the perlvar tutorial in doubt. Windows is usually MSWin32.
      thank you for the fast answers.

      But this seems not to be the solution for me. I try it already with the following code...

      if ( not $^O eq aix ) {
      use Win32::NetResource;

      If I start the program in Unix I get an error message:
      "Can't locate Win32/ in ... "

        The problem is that the use Win32::NetResource is being executed at compile-time, while the condition is tested later, at runtime.

        See CountZero's reply for a way around it:  the module "if".

        (Also, the string "aix" would need to be quoted... —> always use strict; )

        Update: another way would be to use require (which is being executed at runtime):

        unless ( $^O eq "aix" ) { require Win32::NetResource; Win32::NetResource->import(); # if you want any imports }

        Checking for specific values of $^O is fine if you are absolutely sure that your code will only ever run on the few platforms you check, but what if someone (maybe you) in the future tries to run your code on some other Unix variant than AIX? Then it'll just break again.

        Devel::CheckOS can solve that for you:

        use Devel::CheckOS qw(os_is); if(os_is('MicrosoftWindows')) { eval 'use Win32::NetResource'; } elsif(os_is('Unix')) { # load something Unix-specific } else { # just in case someone tries to use VMS or BeOS or ... die("$^O is not supported\n"); }
Re: Program for Windows and Unix
by CountZero (Bishop) on Feb 27, 2009 at 07:32 UTC
    use if $^O eq 'MSWin32', 'Win32::NetResource';

    Update: added semi-colon


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Error-Message: "use" not allowed in expression at ... syntax error ... near "use if"
        install if , its a module