Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: use vs. require in LWP::Simple

by ton (Friar)
on Apr 22, 2001 at 12:50 UTC ( [id://74552]=note: print w/replies, xml ) Need Help??


in reply to use vs. require in LWP::Simple

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

Replies are listed 'Best First'.
Re: Re: use vs. require in LWP::Simple
by Xxaxx (Monk) on Apr 22, 2001 at 13:50 UTC
    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: note [id://74552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found