J
jl_post
Dear Perl community,
Recently I had to debug a Perl script in a Windows environment that was meant for a Unix environment. The script used the module Device::SerialPort, and since I didn't have it on my Windows installation of Strawberry Perl, the compiler check "perl -c script.pl" was giving me errors.
No problem, I thought; I'll just use the "cpan Device::SerialPort" to install it, and that'll be the end of my problem. Well, I wasn't able to install the Device::SerialPort module through Strawberry Perl, so I looked around CPAN and the internets to see what I could do.
I discovered a very similar module named Win32::SerialPort. It is so similar to Device::SerialPort that many of the method names are the same. And I was able to install Win32::SerialPort onto my Strawberry Perl setup. After that I was able to change:
use Device::SerialPort;
my $port = Device::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
to:
use Win32::SerialPort;
my $port = Win32::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
and the rest of the code (that involved the SerialPort, at least) compiled just fine.
However, once I finished debugging the script I had to remember to change the "Win32::SerialPort" module to "Device::SerialPort" before I sent the script back for use on a Unix platform. Eventually an idea came to me thatI could let the script determine which module to use (depending on whetherthat module existed). In other words, if Device::SerialPort exists, use that one, but if not, use Win32::SerialPort. (And if neither exist, exit/die with an informative error message.)
So my question is: How can I use a module if it is installed, or another if it is not installed?
I want to be able to do something like this:
my $port = do
{
if (use Device::SerialPort)
{
Device::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
}
elsif (use Win32::SerialPort)
{
Win32::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
}
else
{
die "This script requires the Device::SerialPort or Win32::SerialPort module.\n";
}
};
Of course this code won't compile, so I was wondering if someone knows how to get a Perl script to do what I want -- that is, to detect which modules (out of several) exist, and to properly load them as if I used them withnormal "use" syntax.
Thanks in advance,
-- Jean-Luc
Recently I had to debug a Perl script in a Windows environment that was meant for a Unix environment. The script used the module Device::SerialPort, and since I didn't have it on my Windows installation of Strawberry Perl, the compiler check "perl -c script.pl" was giving me errors.
No problem, I thought; I'll just use the "cpan Device::SerialPort" to install it, and that'll be the end of my problem. Well, I wasn't able to install the Device::SerialPort module through Strawberry Perl, so I looked around CPAN and the internets to see what I could do.
I discovered a very similar module named Win32::SerialPort. It is so similar to Device::SerialPort that many of the method names are the same. And I was able to install Win32::SerialPort onto my Strawberry Perl setup. After that I was able to change:
use Device::SerialPort;
my $port = Device::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
to:
use Win32::SerialPort;
my $port = Win32::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
and the rest of the code (that involved the SerialPort, at least) compiled just fine.
However, once I finished debugging the script I had to remember to change the "Win32::SerialPort" module to "Device::SerialPort" before I sent the script back for use on a Unix platform. Eventually an idea came to me thatI could let the script determine which module to use (depending on whetherthat module existed). In other words, if Device::SerialPort exists, use that one, but if not, use Win32::SerialPort. (And if neither exist, exit/die with an informative error message.)
So my question is: How can I use a module if it is installed, or another if it is not installed?
I want to be able to do something like this:
my $port = do
{
if (use Device::SerialPort)
{
Device::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
}
elsif (use Win32::SerialPort)
{
Win32::SerialPort->new($portName)
or die "Can't establish connection with $portName.\n";
}
else
{
die "This script requires the Device::SerialPort or Win32::SerialPort module.\n";
}
};
Of course this code won't compile, so I was wondering if someone knows how to get a Perl script to do what I want -- that is, to detect which modules (out of several) exist, and to properly load them as if I used them withnormal "use" syntax.
Thanks in advance,
-- Jean-Luc