Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

use vs. require in LWP::Simple

by Xxaxx (Monk)
on Apr 22, 2001 at 12:05 UTC ( [id://74551]=perlquestion: print w/replies, xml ) Need Help??

Xxaxx has asked for the wisdom of the Perl Monks concerning the following question:

I'm working with LWP::Simple.

I have a small subroutine which will read in a webpage. Since I'm only using this subroutine on rare occasions I've decided to use 'require' rather than 'use' so that I'm not always eating the overhead of loading the full LWP module.

I am given to understand that require will load a module when the line is encountered during program flow and 'use' will load the module at the beginning. Please clue me in if that's not true.

The following code works just fine:

#!/usr/local/bin/perl -w use strict; use LWP::Simple; my ($content) = get('http://www.newbie.org/index.html'); print $content;
As one would expect. However this code does not work:
#!/usr/local/bin/perl -w use strict; require LWP::Simple; my ($content) = get('http://www.newbie.org/index.html'); print $content;
I can make the code work just fine if I change it to:
#!/usr/local/bin/perl -w use strict; require LWP::Simple; my ($content) = LWP::Simple::get('http://www.newbie.org/index.html'); print $content;
So, I can make the routine run okay.
But sometimes just poking around until stuff works is not enough. It would be nice to understand better why the middle form of 'require' without the 'LWP::Simple::' in front of the get doesn't work.

Thanks in advance
Claude

Replies are listed 'Best First'.
Re: use vs. require in LWP::Simple
by ton (Friar) on Apr 22, 2001 at 12:50 UTC
    You are correct in what require does: it loads the given module at runtime. use when applied to a package name is equivalent to
    BEGIN { require <package> import <package> [<arguments>] }
    The BEGIN wrapper is what makes the statement execute when it is encountered. Perl loads eveything found in a BEGIN subblock before all other code in that block. The real difference is the import function, which is defined by the package itself (usually through inheriting from Exporter). import is responsible for taking functions and variables defined in that package and importing them to the calling package's namespace. So in your example, import takes the get function defined in LWP::Simple and imports it to the main package... so that now you no longer need the fully qualified name. Make sense?

    Hope this helps,

    -Ton
    -----
    Be bloody, bold, and resolute; laugh to scorn
    The power of man...

      Thanks for the explanation.
      I made the experiment of putting an 'import' in the routine.

      require LWP::Simple; import LWP::Simple; my($content) = get($url);
      Now the get works without the explicit package namespace.

      Not sure what if any damage I'm doing using an import with zero idea of proper arguement. So I'll keep the explicit use of LWP::Simple namespace for the moment.

      Thanks again
      Claude

        The arguments to import or use are simply (for most modules) the names of the subs or variables to be imported. It is generally preferable to only import the things you will use, so as to avoid cluttering the main:: namespace. On the other hand sometimes you will want to import things that are not imported by default.

        You probably want

        use LWP::Simple 'get'; # or require LWP::Simple; import LWP::Simple 'get'; # which actually parses as LWP::Simple->import('get');

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-24 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found