Cross Platform Perl

Rob Casey rob at
Wed Oct 16 22:27:37 CDT 2002

Just a follow up to this ...

> BEGIN {		# So we run at the same time as most use
> 	if ($^O eq 'MSWin32') {
> 		eval { use My::Module; }
> 	}
> }

This will fail - As the compiler will find this use statement and
attempt to include the module at this point.  To make use of dynamic
loading of modules, the eval statement must called with a string
argument as this string is not interpreted at compile time, but at the
time of execution.


Rob Casey
Business Manager, Senior IT Consultant
Cowsnet Internet and Professional Services


-----Original Message-----
From: owner-melbourne-pm at [mailto:owner-melbourne-pm at] On
Behalf Of Paul Fenwick
Sent: Thursday, 17 October 2002 1:02 PM
To: Adam Clarke
Cc: melbourne-pm at
Subject: Re: Cross Platform Perl

G'day Adam,

On Thu, Oct 17, 2002 at 12:42:42PM +1000, Adam Clarke wrote:

> I'm trying to write a script that runs on Linux (Unix) and Win32. I
> to /use/ a module when on Win32 (to access the registry) that doesn't 
> exist in Unixy perl. Is there a way to skip a use statement at runtime

> based on platform or is the only/best way to make a full build 
> (MakeMaker thing) and handle the differences there somehow. If the 
> latter, anyone got some simple pointers.

Glad you asked!  Perl has a variable called $^O (or $OSNAME when
using English) that gives you the name of the operating system
you're running under.

So you can do the following:

BEGIN {		# So we run at the same time as most use statements.
	if ($^O eq 'MSWin32') {
		eval { use My::Module; }

The eval is needed because usually use statements get executed
immediately, regardless of context.  The BEGIN means that your
chunk of code is executed "at compile time".  If this isn't important,
you can leave the BEGIN block out.

There's lots of code out there which does different things depending
upon the OS.  Take a look at File::Copy's source code
(perldoc -m File::Copy) to see a lot of this in action.



Paul Fenwick <pjf at> |
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3222 bytes
Desc: not available
Url :

More information about the Melbourne-pm mailing list