Home: Perl Programming Help: Intermediate:
How can i use a moudle only when i need it



newbie
Deleted

Apr 20, 2000, 12:10 PM


Views: 890
How can i use a moudle only when i need it

How can i use a module only when i need it instead of putting the use module at the top of the script


japhy
Enthusiast

Apr 21, 2000, 9:05 AM


Views: 890
Re: How can i use a moudle only when i need it

The require() function will do what you need:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


sub foo {
require SomeModule;
import SomeModule;
# and then you can act as though
# you'd done: use SomeModule;
}
</pre><HR></BLOCKQUOTE>


Cure
User

Apr 21, 2000, 1:16 PM


Views: 890
Re: How can i use a moudle only when i need it

Hi

* "use" is evaluated at compile time.
* "eval {}" compiles the block at compile time.

eval 'use Package';

which while being useful from the perspective of deferring the use
until runtime, also fails to do the import until runtime, so
prototypes and bareword-subroutines will fail. As long as you're going
that far, Like this:

if ($some_condition) {
require Package;
}

and then use the full path to the subroutines, since the importer will
not have executed. Just like "use Package ()". And notice, no tricky
eval involved.

And like Japhy said you can use the require function. thereby getting the whole thing.

if ($some_condition) {
require Package;
import Package qw/.../;
}

Cure

[This message has been edited by Cure (edited 04-21-2000).]