Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
A mystery of conditional loadingby Llew_Llaw_Gyffes (Scribe) |
on Mar 23, 2009 at 00:59 UTC ( [id://752462]=perlquestion: print w/replies, xml ) | Need Help?? |
Llew_Llaw_Gyffes has asked for the wisdom of the Perl Monks concerning the following question: Brethren, I seek the benefit of your experience. I have a Perl application, a multithreaded client for the ICB chat service, to which I recently added client-to-client encryption as a feature. Now, the base client requires only about two modules that are not normally part of the default Perl5 install. This, I considered acceptable. But adding encryption required the addition of about another eight modules. Since I did not know whether users would wish to install the additional eight modules for a feature they might not use, I sought a way to make use of those modules optional. And indeed, eventually I found such a means: Module::Load::Conditional. After some study, I wrote the following code:
And lo, this code would reportedly load all the necessary modules if available, or, if one or more modules were not available, it would disable the encryption feature - but continue execution anyway. Except that later on, a call to decode_base64() would fail. Thread 3 terminated abnormally: Undefined subroutine &main::decode_base64 called at ./icbm.conditional-load line 2071.That line is in the middle of the function below:
But lo, did not Module::Load::Conditional say all modules had loaded? Doubting its word, I sought a means to verify that they were indeed loaded. I discovered that Module::Loaded, while it did not contain any function to directly tell me whether a module was loaded, would — if I instructed it to mark those modules as loaded — carp if it found them already loaded. Thus I added the following code, right after the Module::Load::Conditional invocation above:
And lo, it reported all these modules were, indeed, already loaded, just as Module::Load::Conditional had said:
Brothers, I am perplexed. I am assured, doubly, that MIME::Base64 is loaded. And the other modules loaded with it are clearly loaded, or my code would fail much sooner (not least at the invocation of Crypt::CBC two lines earlier). Indeed, this is not even the first use of MIME::Base64 in the code. So why, then, can I not call decode_base64(), when that code worked perfectly when I was loading it with a simple "Use MIME::Base64;"?
Back to
Seekers of Perl Wisdom
|
|