Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

BEGIN simplifies multi-platform programs

by blssu (Pilgrim)
on Sep 13, 2002 at 14:33 UTC ( [id://197596]=CUFP: print w/replies, xml ) Need Help??

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 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 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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-07-14 20:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.