use modules OS dependent

I'm writing an perl script which should communicate over a serial
port. The script should be able to run in Linux and Win32
environments. In both environments exist modules to access the serial
port:

Win32::SerialPort (under Windows)
Device::SerialPort (else)

I know it is possible to detect the OS using Config::Config. But it
seems not possible to use different modules depending on this
information like this:

if ( $OS eq 'WINDOWS' ) {
use Win32::SerialPort qw( :PARAM :STAT );
} else {
use Device::SerialPort qw( :PARAM :STAT );
}

What's the right way to write such an OS dependent application?

Steffen

PS: Fup to comp.lang.perl.modules
sledz [ Mo, 14 April 2008 15:24 ] [ ID #1942300 ]

Re: use modules OS dependent

sledz [at] dresearch.de wrote:
> I'm writing an perl ...

I just answered your question in clpmodules. Please show some patience.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson [ Mo, 14 April 2008 15:26 ] [ ID #1942301 ]

Re: use modules OS dependent

sledz [at] dresearch.de wrote:
>I'm writing an perl script which should communicate over a serial
>port. The script should be able to run in Linux and Win32
>environments. In both environments exist modules to access the serial
>port:
>
> Win32::SerialPort (under Windows)
> Device::SerialPort (else)
>
>I know it is possible to detect the OS using Config::Config. But it
>seems not possible to use different modules depending on this
>information like this:
>
> if ( $OS eq 'WINDOWS' ) {
> use Win32::SerialPort qw( :PARAM :STAT );

This is explained about halfway down in the documentation for "use".

The "BEGIN" forces the "require" and "import" to happen at
compile time.

>What's the right way to write such an OS dependent application?

Don't use use() but an explicit require() and import() without the
enclosing BEGIN.

jue
jurgenex [ Mo, 14 April 2008 15:59 ] [ ID #1942363 ]

Re: use modules OS dependent

Jürgen Exner wrote:
> sledz [at] dresearch.de wrote:
>> What's the right way to write such an OS dependent application?
>
> Don't use use() but an explicit require() and import() without the
> enclosing BEGIN.

s/without/with/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Gunnar Hjalmarsson [ Mo, 14 April 2008 16:13 ] [ ID #1942364 ]
Perl » comp.lang.perl.misc » use modules OS dependent

Vorheriges Thema: Gisle Aas's Illustrated Perl Guts?
Nächstes Thema: FAQ 9.11 How do I redirect to another page?