I'd suggest a similar trick to what we do in C/C++ when the wrapped library is too large to individually load each function. Cheat.
And then, in your module, just do something like this:package Wrap::PDL; use PDL; use base 'Exporter'; # for simplicity... sub my_method { # Code } 1;
Well, either that, or just require PDL to be installed :-Pmy $has_PDL = eval "use Wrap::PDL qw(my_method); 1"; #... if ($has_PDL) { my_method(0, 0, $value); }
Update: Fletch pointed out that I had $has_PDL in double-quotes and needed either backwhacking or to be in single-quotes. So I removed it altogether. This method takes advantage of tye's trick on ensuring whether an eval executes properly or not by checking its return code rather than $@, and ensuring that correct execution actually does result in a true value. I can also be more confident in using lexical variables here. I'm pretty sure eval STR can use lexicals declared outside of its scope, but here I don't even have to think about that.
Another alternative would be:
which is probably better in that I prefer using eval BLOCK over eval STRING whenever possible.my $has_PDL = eval { require Wrap::PDL; Wrap::PDL->import(qw(my_method)); 1; }
In reply to Re: Conditional loading of methods
by Tanktalus
in thread Conditional loading of methods
by bruno
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |