Click here to get back home

use modules OS dependent

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
use modules OS dependent sledz 04-14-2008
Posted by sledz on April 14, 2008, 8:42 am
Please log in for more thread options
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

Posted by Gunnar Hjalmarsson on April 14, 2008, 9:22 am
Please log in for more thread options
sledz@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 );
> } else {
> use Device::SerialPort qw( :PARAM :STAT );
> }
>
> What's the right way to write such an OS dependent application?

I don't know about "right", but this is one way:

BEGIN {
if ( $^O eq 'MSWin32' ) {
require Win32::SerialPort;
import Win32::SerialPort qw( :PARAM :STAT );
} else {
require Device::SerialPort;
import Device::SerialPort qw( :PARAM :STAT );
}
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by sledz on April 14, 2008, 9:40 am
Please log in for more thread options
> I don't know about "right", but this is one way:
> ...

Thanx Gunnar. That was the solution. And sorry for my impatience. ;-)

Posted by Petr Vileta on April 15, 2008, 6:35 pm
Please log in for more thread options
Gunnar Hjalmarsson wrote:
> sledz@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:
>>

<snip>

> if ( $^O eq 'MSWin32' ) {

I prefer
if ( $^O =~ /Win32/i ) {

--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your
mail from another non-spammer site please.)

Please reply to <petr AT practisoft DOT cz>


Similar ThreadsPosted
Re: use modules OS dependent April 14, 2008, 9:59 am
bundle up dependent modules July 21, 2004, 3:11 pm
GD modules September 1, 2004, 8:20 am
Perl PDF modules - help please August 11, 2004, 2:31 pm
installing modules? September 29, 2005, 1:52 pm
Need new maintainers for my modules January 8, 2005, 7:24 am
3rd party modules March 23, 2005, 4:56 pm
Installing modules August 5, 2005, 8:32 pm
Captcha Modules August 15, 2005, 5:35 pm
writing modules in 'c' August 27, 2005, 3:18 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap