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

I have a question concerning something I've never really thought about. Is there a method in Perl that allows a programmer to have certain pieces of code execute depending on what system architecture that code resides on? For example:
if (on Win32) { do this; } else { # Assume unix box do that; }
Anyone have any ideas?

Replies are listed 'Best First'.
Re: Porting code between NT/Unix
by chipmunk (Parson) on Dec 08, 2000 at 03:21 UTC
    Yes, the $^O variable is what you're looking for. (That's a caret and a capital o.)
    if ($^O =~ /Win32/i) { do this; } else { do that; }
    Head on over to perlvar to read more about this special variable.
Re: Porting code between NT/Unix
by myocom (Deacon) on Dec 08, 2000 at 03:21 UTC
    The $^O special variable will tell you the OS you're running on, so you could do something like:
    if ($^O eq 'MSWin32') { # do something Win32ish } else { # do something Unixy }
    See also perlfaq8.
Re: Porting code between NT/Unix
by eg (Friar) on Dec 08, 2000 at 11:05 UTC

    I suppose you really man 'operating system' rather than 'system architecture', but just in case, you can find the latter with Config.pm

      use Config;
    
      if ( $Config{archname} eq whatever ) {
        ...
      }
    

    This'll allow to differentiate between linux on alpha and linux on sgi or Dreamcast or whatever.

    Of course see the Config perldoc for more details.