Re: How to 'conditionally' use a specified module
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Thu, 16 Nov 2006 13:38:48 -0500
"d" == danny <dannymiles2@xxxxxxxxx> writes:
d> 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.
>>
d> Hi Uri,
d> Thanks for the advise. Unfortunately I can't use require because the
d> code I fixed is inside a module and I can only reference with
d> MyPackage::This and MyPackage::That.
huh?? require can load any module just like use. they share the same
syntax for that! require can also load a file name (not just a bare
class name).
d> In my particular situation, David Filmer's tip worked perfectly.
d> my $use_cache = 'no';
d> use if ($use_cache eq 'yes'), 'Cache::SizeAwareFileCache';
d> That fixed everything.
d> But I'll remember your advise in case next time I needed to use
d> something like this and couldn't get it to work with if pragma.
are you sure that worked as you expect? that code doesn't prove it WILL
load when it is 'yes', just that it won't load if it is 'no'. my
conjecture is that is it wrong as the use line will execute BEFORE the
assignment to $use_cache. check it out and see.
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.
- Follow-Ups:
- References:
- How to 'conditionally' use a specified module
- From: danny
- Re: How to 'conditionally' use a specified module
- From: usenet
- Re: How to 'conditionally' use a specified module
- From: Uri Guttman
- Re: How to 'conditionally' use a specified module
- From: danny
- How to 'conditionally' use a specified module
- Prev by Date: Perl - Joining of Binary values
- Next by Date: Re: subroutine call makes foreach exit?
- Previous by thread: Re: How to 'conditionally' use a specified module
- Next by thread: Re: How to 'conditionally' use a specified module
- Index(es):
Relevant Pages
|