Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

BEGIN simplifies multi-platform programs

by blssu (Pilgrim)
on Sep 13, 2002 at 14:33 UTC ( #197596=snippet: print w/replies, xml ) Need Help??
Description: 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"))'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://197596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2021-10-26 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (90 votes). Check out past polls.

    Notices?