in reply to how to use different packages depending on host OS?
I can think of at least three variants:
or the manual way of doing what use does:my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; # /win/ will make Darwin (OSX) users unhappy :-) eval "use $module" or die "$module didn't return a true value"; die $@ if $@;
BEGIN{ if ($^O =~ /MSWin32/) { require Win32::Foo; Win32::Foo->import(); } else { require Unix::Bar; Unix::Bar->import(); }; };
Or, if you are not afraid to use modules conceived by Michael Schwern, and you do load modules from several places, like in a plugin setting:
use UNIVERSAL::require; my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; $module->require; $module->import(1,2,3);
There also is the way of completely emulating what use does:
BEGIN { my $module = $^O =~ /MSWin32/ ? "Win32::Foo" : "Unix::Bar"; my $file = $module . ".pm"; $file =~ s!::!/!g; require $file or die "$file didn't return a true value"; $module->import(); };
So many options - the most sane ones are upper in this post though.
Update: Added the fully-manual way of use emulation
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to use different packages depending on host OS?
by smullis (Pilgrim) on Oct 27, 2004 at 11:36 UTC |