If you have a program that must run on several platforms, use BEGIN to load platform specific versions of your utility routines. This eliminates tests in your main code and cleanly isolates non-portable code.
## file myapp
use strict;
BEGIN {
if ($^O eq 'MSWin32') {
require MyAppWin32
}
else {
require MyAppUnix
}
}
print "HOME is '", get_home_dir(), "'\n";
## file MyAppWin32.pm
package main;
use Win32::TieRegistry ( Delimiter => '/' );
sub get_home_dir {
my $key = $Registry->{'HKEY_CURRENT_USER/Software/Microsoft/Window
+s/' .
'CurrentVersion/Explorer/Shell Folders'};
scalar($key->GetValue('Personal'))
}
1;
## file MyAppUnix.pm
package main;
sub get_home_dir {
(getpwuid($<))[7]
}
1;