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;

Replies are listed 'Best First'.
Re: BEGIN simplifies multi-platform programs
by rob_au (Abbot) on Jan 19, 2003 at 12:01 UTC
    This can be easily performed with the if module - For example:

    use if ( $^O eq 'MSWin32' ), 'MyAppWin32'; use if ( $^O ne 'MSWin32' ), 'MyAppUnix';

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000011100"))'