Loading library different per Operating System

Loading library different per Operating System

am 11.06.2011 17:20:13 von JP

Hi all,

I need to load a different library, depending on the platform I am running on.

When running on Linux, I need "Device::SerialPort";
while running on Windows I need "Win32::SerialPort".

This is what I came up with:

if ( $OS eq "linux" ) { use Device::SerialPort qw( :PARAM :STAT 0.07 ); }
elsif ( $OS eq "MSWin32" ) { use Win32::SerialPort qw( :PARAM :STAT 0.07 ); }
else { die "Unsupported Operating System: $OS\n"; };

But the loading of the libraries is done during compile and therefore this will allways fail with an "Can't locate ..../SerialPort.pm in @INC".\

How can I load a library depending on the platform I run on?

Thanks!

JP

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Loading library different per Operating System

am 12.06.2011 01:51:01 von Shawn H Corey

On 11-06-11 11:20 AM, JP wrote:
> How can I load a library depending on the platform I run on?

Try:

BEGIN {
use English qw( -no_match_vars );

if ( $OSNAME eq "linux" ) {
use Device::SerialPort qw( :PARAM :STAT 0.07 );
}
elsif ( $OSNAME eq "MSWin32" ) {
use Win32::SerialPort qw( :PARAM :STAT 0.07 );
}
else {
die "Unsupported Operating System: $OSNAME\n";
};
}



--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Loading library different per Operating System

am 12.06.2011 03:13:39 von rvtol+usenet

On 2011-06-11 17:20, JP wrote:

> How can I load a library depending on the platform I run on?

A use statement is always processed at compile time, no matter if there
are BEGIN blocks or if statements around it, or where it is in the source.

Check out "use if" (see perldoc if), or do the string-evalling yourself.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Loading library different per Operating System

am 12.06.2011 10:14:24 von JPH

Here is how I solved it:

use if $^O eq "linux" , "Device::SerialPort" => qw( :PARAM :STAT 0.07 );
use if $^O eq "MSWin32" , "Win32::SerialPort" => qw( :PARAM :STAT 0.19 );

Thanks!

On 06/12/2011 03:13 AM, Dr.Ruud wrote:
> On 2011-06-11 17:20, JP wrote:
>
>> How can I load a library depending on the platform I run on?
>
> A use statement is always processed at compile time, no matter if there are BEGIN blocks or if statements around it, or where it is in the source.
>
> Check out "use if" (see perldoc if), or do the string-evalling yourself.
>

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/