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

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.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://746797]
[LanX]: darn trying to downvote him again fails ... ;-P
[Discipulus]: congrats!
[LanX]: His Excelleny Choroba will now nbe clothed in his new robe ...
[Discipulus]: bishop ship sheeps in the shop..
[1nickt]: Discipulus too easy!
[1nickt]: "The sixth sheik's sixth sheep's sick" (if you are after 'sh' tongue twisters)
[Discipulus]: ok try it in Eatalian ;=)
[Eily]: Discipulus I don't suppose you can do the same for me can you ? :P
[Discipulus]: Eily tongue twister or votes? ;=)
[Eily]: Discipulus turn me into a Bishop

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2017-09-26 12:02 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (294 votes). Check out past polls.