Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using 'use' away from script start

by cLive ;-) (Prior)
on Nov 15, 2001 at 04:23 UTC ( [id://125469]=perlquestion: print w/replies, xml ) Need Help??

cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

greetings...

If I'm only "use"ing a module occasionally in a script (eg, only in one sub that is rarely called), is it better performance-wise to place the "use" statement in that sub? ie:

#!/usr/bin/perl -w use strict; ... sub once_in_a_blue_moon { use LWP::Simple; my $page = get('http://www.yahoo.com'); }
Or are there other issues I may encounter with this approach?

More a "hmmmm?" than a "Argh, I must know!!!", but I'm sure you understand...

cLive ;-)

"After my grandfather became ill, my grandmother greased his back. After that, he went downhill very quickly" - Milton Jones

Replies are listed 'Best First'.
(Ovid) Re: Using
by Ovid (Cardinal) on Nov 15, 2001 at 04:28 UTC

    No. use happens at compile time and your example will always use the module. Use this:

    sub once_in_a_blue_moon { require LWP::Simple; LWP::Simple->import; my $page = get('http://www.yahoo.com'); }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Using 'use' away from script start
by ehdonhon (Curate) on Nov 15, 2001 at 08:48 UTC

    As Ovid mentioned, Perl does all the use'ing at compile time no matter where it lives in your code. As an insteresting aside, it turns out that it will even evaluate use statements prior to optimizing your code. So even if you do something like this:

    if ( 0 ) { use MyModule; }

    Perl will still load MyModule.

    If you really only want to load that module whenever that function is called, then you need to require() the module instead of use'ing it.

Re: Using 'use' away from script start
by Fletch (Bishop) on Nov 15, 2001 at 07:54 UTC

    You might also consider using AutoLoader or SelfLoader and remove the rarely used code from your main program to begin with. Then if/when it ever does get called it and its requirments will get pulled in.

Re: Using 'use' away from script start
by jmcnamara (Monsignor) on Nov 15, 2001 at 13:29 UTC

    For this type of situation I prefer the autouse pragma:     use autouse 'Some::Module' => 'some_function';

    This defers the loading of the module until the function is called.

    However, It doesn't seem to work for LWP::Simple::get. I think that this is due to the fact that LWP::Simple defines its own import().

    --
    John.

Re: Using 'use' away from script start
by MZSanford (Curate) on Nov 15, 2001 at 14:28 UTC
    All of this, and require goes unloved. :(

    In the spirit of KISS, and also guessing that if loading LWP::Simple is too much a performance penalty, i can't imagine Autoloader will be well recieved, i put forward something like this :
    sub once_in_a_blue_moon { require LWP::Simple; my $page = get('http://www.yahoo.com'); }

    please note, i suggest use'ing the module at the top to improve readability, as i think that will be more important in the long run that the minor gain of not loading LWP::Simple.
    i had a memory leak once, and it ruined my favorite shirt.
      Have a look at the first reply here, MZSanford, you'll see that require is getting plenty of lovin'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://125469]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2025-02-14 07:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found