Hello monks,
I've built an application which is designed to run both normally on Linux, or packaged up on Win32 with Cava Packer. Cava Packer comes with Cava::Pack, which I need to fake out, because as far as I can tell, there is no equivalent module for Linux.
I've worked around this with the following code:
BEGIN {
package Cava::Pack;
sub Resource { $Cava::Pack::dir.'/'.$_[0]; }
sub SetResourcePath { $Cava::Pack::dir = $_[0]; }
sub GetInfoProductVersion { "0.9.9.0"; }
}
[download]
This works fine in Linux, but in Windows I need to use the real Cava::Pack, and if I add a use Cava::Pack; line, I get function redefinition warnings (not to mention compilation fails on Linux because there is no Cava/Pack.pm in my @INC.
What I need to do is conditionally use the real Cava::Pack if it is available, or run my above code if it is not available. How do I go about that?
BEGIN {
if( eval { require Cava::Pack; 1 } ) {
import Cava::Pack;
} else {
no strict 'refs';
*Cava::Pack::Resource = sub { $Cava::Pack::dir . '/' . $_[0]; };
*Cava::Pack::SetResourcePath = sub { $Cava::Pack::dir = $_[0]; };
*Cava::Pack::GetInfoProductVersion = sub { "0.9.9.0"; };
} ## end unless ( eval { require Cava::Pack...})
} ## end BEGIN
[download]]
# add Cava\Pack.pm at the end of include path,
# real Cava::Pack is found earlier
push @INC, './dummy'
use Cava::Pack
[download]
BEGIN { push @INC, './dummy'; }
[download]
use lib './dummy';
[download]
Lots
Some
Very few
None
Results (215 votes),
past polls