in reply to Re: Easing cross-platform scripting
in thread Easing cross-platform scripting
module that loads either Win32::SerialPort or Device::SerialPort, although they both provide a very similar APII've done something like that...not an "Any" module, but conditionally loading one or the other. You do need to wrap it in a BEGIN block and eval, due to the way "use" works:
Then something like this when using it:# # Use Win32::Serial port on Windows otherwise, use Device::SerialPort #; BEGIN { my $IS_WINDOWS = ($^O eq "MSWin32" or $^O eq "cygwin") ? 1 : 0; # if ($IS_WINDOWS) { eval "use Win32::SerialPort"; die "$@\n" if ($@); } else { eval "use Device::SerialPort"; die "$@\n" if ($@); } }
Somewhat ugly, but handy for anyone using my module. There's likely some more elegant way to do it.my $IS_WINDOWS = ($^O eq "MSWin32" or $^O eq "cygwin") ? 1 : 0; if ($IS_WINDOWS) { $serial = new Win32::SerialPort ($port, 1); } else { $serial = new Device::SerialPort ($port, 1); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Easing cross-platform scripting (conditional module loading)
by hippo (Archbishop) on Nov 27, 2018 at 22:38 UTC | |
|
Re^3: Easing cross-platform scripting
by haukex (Archbishop) on Nov 28, 2018 at 15:30 UTC |