Re: How to 'conditionally' use a specified module



danny wrote:
Uri Guttman wrote:
"u" == usenet <usenet@xxxxxxxxxxxxxxx> writes:

u> use if ($use_cache eq 'yes'), Cache::SizeAwareFileCache;

that will still fail if the boolean value is set at runtime since use
executes at compile time. this is where require is better as it only
executes at runtime. (of course string eval breaks all those rules).

so to the OP, you need to call require and not use there.


Thanks for the advise. Unfortunately I can't use require because the
code I fixed is inside a module and I can only reference with
MyPackage::This and MyPackage::That.

I have no idea what makes you think you can't use require. Can you
explain?

In my particular situation, David Filmer's tip worked perfectly.

my $use_cache = 'no';
use if ($use_cache eq 'yes'), 'Cache::SizeAwareFileCache';
That fixed everything.

No, it really didn't. If you think it did, you didn't test well
enough. Your statement that assigns 'yes' or 'no' to $use_cache
executes at runtime. The if pragma executes at compile time. At the
time you check to see if $use_cache is equal to 'yes', $use_cache is
undefined. If you were using warnings, Perl would have told you this.
Change
my $use_cache = 'no';
to
my $use_cache = 'yes';
and you will see that the module is still not loaded.

Paul Lalli

.



Relevant Pages

  • Re: How to conditionally use a specified module
    ... u> danny wrote: ... that will still fail if the boolean value is set at runtime since use ... executes at compile time. ...
    (perl.beginners)
  • Re: Still not getting it.
    ... Compile time for the creator, ... Vector executes is creates a definition. ... 1024 Vector stimulus OK ... gain and response arrays. ...
    (comp.lang.forth)
  • Re: How to conditionally use a specified module
    ... that will still fail if the boolean value is set at runtime since use ... executes at compile time. ...
    (perl.beginners)
  • Re: How to process contents of a list variable through a macro ?
    ... > But the compiler executes the Macros during compile time. ... A DEFPARAMETER does get executed at compile time. ... It is a DEFVAR declaration that dosn't. ...
    (comp.lang.lisp)
  • VB6 SP6 BUG Using NOT keyword??!!!
    ... I have a function (which returns a Boolean) call that checks for an existing Internet connection, and executes a block of code if the connection does not exist, as such: ... Whenever the CheckInternet function returns TRUE, the condition executes anyway. ... Dim bResult As Boolean ...
    (microsoft.public.vb.bugs)