in reply to Program for Windows and Unix

You may use $^O or $OSNAME to determine the operating system.
Check the perlvar tutorial in doubt. Windows is usually MSWin32.

Replies are listed 'Best First'.
Re^2: Program for Windows and Unix
by Richi (Initiate) on Feb 27, 2009 at 07:37 UTC
    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 }
        Or you could eval 'use Win32::NetResource'.
        Thank you!
        require is working!

      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"); }