in reply to Perl in a multi-platform environment

Another way to do this in Perl.
$system=`uname`; 
if ($system=~m/Linux/i)
    {print "Linux\n";
    }
elsif ($system=~m/Solaris/)
    {
    print "Solaris\n";
    }
elsif ($system=~m/AIX/)
    {
    print "AIX\n";
    }
else {
     print "Unknown OS\n";
    }
Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)
  • Comment on Re: Perl in a multi-platform environment

Replies are listed 'Best First'.
Re^2: Perl in a multi-platform environment
by perlfan (Parson) on Jul 18, 2014 at 02:55 UTC
    Perl already knows your OS, $^O.

    And are you kidding me with that if/else travesty?

    If a small scale change, use a hash of subroutine references to make dispatching easy.

    If you have time, create a parent class and subclass out to each OS (again, if you really have to do different things on each OS).

    use strict; my $os_dispatch = { linux => sub {print qq{Linux\n}}, solaris => sub {print qq{Solaris\n}}, aix => sub {print qq{AIX\n}}, freebsd => sub {print qq{FreeBSD\n}}, darwin => sub {print qq{MacOSX\n}}, unknown => sub {print qq{Unknown\n}}, }; if (exists $os_dispatch->{$^O}) { $os_dispatch->{$^O}->(); } else { $os_dispatch->{unknown}->(); }
    Finally, do you really have to do that much OS specific stuff?