In general, you're better off using Devel::CheckOS instead of looking directly at $^O.

Second, I recommend putting all your platform-specific code into platform-specific modules. So, for example ...

use Devel::CheckOS qw(os_is); if(os_is('MSWin32')) { eval 'use MyApplication::Platform::Win32'; } elsif(os_is('MacOSX')) { eval 'use MyApplication::Platform::MacOSX'; } else { warn "not MSWin32 or MacOSX, falling back to defaults\n"; eval 'use MyApplication::Platform::Default'; }
Your MyApplication::Platform::* modules should then export the same set of subroutines that wrap up the platform-spceific bits, so that the core of your application is exactly the same no matter what platform. For example, they could all export a subroutine check_dir_is_writeable, whose Windows implementation would look like:
use Win32::File; use Win32::FileSecurity; sub check_dir_is_writeable { my $http_rec_localdir = shift; my $attrib; Win32::File::GetAttributes($http_rec_localdir, $attrib); if ($attrib & (Win32::File::SYSTEM | Win32::File::HIDDEN)) ... }
and whose Mac OS X implementation is:
sub check_dir_is_writeable { -d $_ && -w $_ && -x $_ }
Your application then can just do this to have it work on either platform:
if(check_dir_is_writeable($dir)) { # yay! } else { # ohnoes! }

In reply to Re: including modules during runtime and dealing with OS specific (constants) code block by DrHyde
in thread including modules during runtime and dealing with OS specific (constants) code block by periferral

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.