note
DrHyde
<p>In general, you're better off using [cpan://Devel::CheckOS] instead of looking directly at <code>$^O</code>.
<p>Second, I recommend putting all your platform-specific code into platform-specific modules. So, for example ...
<code>
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';
}
</code>
Your <code>MyApplication::Platform::*</code> 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 <code>check_dir_is_writeable</code>, whose Windows implementation would look like:
<code>
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))
...
}
</code>
and whose Mac OS X implementation is:
<code>
sub check_dir_is_writeable {
-d $_ && -w $_ && -x $_
}
</code>
Your application then can just do this to have it work on either platform:
<code>
if(check_dir_is_writeable($dir)) {
# yay!
} else {
# ohnoes!
}
</code>
917920
917920