Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Just another Perl shrine
 
PerlMonks  

Re: Program for Windows and Unix

by weismat (Friar)
on Feb 27, 2009 at 07:28 UTC ( #746797=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Program for Windows and Unix
Re^2: Program for Windows and Unix
by Richi (Initiate) on Feb 27, 2009 at 07:37 UTC
    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 ... "

      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 }
        Thank you!
        require is working!
        Or you could eval 'use Win32::NetResource'.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://746797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-04-20 04:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls