I needed this for a crossplatform project. I found no documentation of the OS strings, so I did a bit of testing and ended up with this:
if ($^O =~ /mswin/i) {# Windows code}
elsif ($^O =~ /linux/i) {# linux code}
elsif ($^O =~ /darwin/i){# mac code}
This is designed to introduce some tolerance just in case some old or weird OS or perl versions produce slightly different OS strings. Seems to work fine so far.
In fact, I even added a fallback in case the OS string doesn't match any of these (let the user decide):
my $OS;
if ($^O =~ /mswin/i) {$OS = "Windows";print "OS detected: Windows\n"}
elsif ($^O =~ /linux/i) {$OS = "Linux";print "OS detected: Linux\n"}
elsif ($^O =~ /darwin/i) {$OS = "Mac";print "OS detected: Mac OS X\n"}
+
else {print "\nUnable to detect OS type, choose your OS:\n\nWindows
+ Any version of Microsoft Windows\nMac Any flavour of Mac OS X\nLi
+nux Linux of some sort\n\n";
do {
chomp ($OS = <STDIN>);
print "\nIncorrect OS type. Try again.\n\n" unless $OS eq "Windows" or
+ $OS eq "Mac" or $OS eq "Linux";} until ($OS eq "Windows" or $OS eq "
+Mac" or $OS eq "Linux");
}
Your "use if" construct is intriguing... My script also uses different modules on different platforms. So far, I've been resorting to changing the code for each platform (I need to produce separate releases for other reasons anyway) becuse
if ($^O =~ /mswin/i) {use module::name;}
didn't work. Maybe I'll try
use if $OS eq 'Windows', module::name;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.