Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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 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: CUFP [id://197596]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-18 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found