in reply to Re: Program for Windows and Unix
in thread Program for Windows and Unix

Hey,
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/NetResource.pm in ... "

Replies are listed 'Best First'.
Re^3: Program for Windows and Unix
by almut (Canon) on Feb 27, 2009 at 07:49 UTC

    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!

        Note that 'require' does not run the 'import' method of the module. You may have to call import() yourself for some modules.

        Note also that if you require the module at runtime it will be too late to run any CHECK or INIT blocks in the module, so they will not be run. Some modules depend on these blocks being run. You can put the code to load the module in a BEGIN block to ensure it is run at compile time and that CHECK and INIT blocks are run.

Re^3: Program for Windows and Unix
by DrHyde (Prior) on Feb 27, 2009 at 10:57 UTC

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