Re: How to 'conditionally' use a specified module



"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
.



Relevant Pages

  • WSE816 error
    ... We have diagnosed the problem to be that the Cisco load balancer that we are ... When a Web service is hosted in a Web farm that uses one URI for the load ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: page_load executes twice ?
    ... when page executes page load second time,, ... > pageload (although page is loading second time ispostback property is ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Compiles without Executing
    ... how you define "logically correct" this might fit the bill. ... To meet the "never executes" criterion, ... OS can't load the program -- again for resource reasons, ...
    (comp.programming)
  • Re: page_load executes twice ?
    ... (like dropdownlist with autopostback property set ... will ispostback property be true or false (in my problem ... when page executes page load second time,, ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: Using # in filename in JScript causes error
    ... The 'load' method takes a URI and URI's need to be escaped if they contain special characters. ... (% = 0x23 in ASCII) ...
    (microsoft.public.scripting.jscript)