CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



newbie
Deleted

Apr 20, 2000, 12:10 PM

Post #1 of 3 (479 views)
How can i use a moudle only when i need it Can't Post

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

Post #2 of 3 (479 views)
Re: How can i use a moudle only when i need it [In reply to] Can't Post

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

Post #3 of 3 (479 views)
Re: How can i use a moudle only when i need it [In reply to] Can't Post

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).]

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives