Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Related question: loading modules at run time

by dash2 (Hermit)
on Jan 23, 2001 at 18:56 UTC ( [id://53694]=note: print w/replies, xml ) Need Help??


in reply to How can I source other perl scripts from within a Perl script

I thought I'd add to this thread rather than start a new one.

I'm writing a little script to check whois servers. One funky feature will be the ability to check for domains which don't have a whois server, only a web-based look up - like .tv domains. To do this, I have a wrapper function which chooses whether to do a normal whois or a web whois. The web whois uses the LWP module, like a good perl script.

Now I don't want to load the LWP module - which is not small, right? - every time I run the script, but only when someone is looking up a .tv domain. Is there a simple way to "use" or "require" a module at run time, without getting compile time exceptions because Perl can't find methods and subroutines from the module within the code?

I have a feeling AUTOLOAD has something to do with this... any deep wisdom would be gratefully received.

Thanks

David

  • Comment on Related question: loading modules at run time

Replies are listed 'Best First'.
Re: Related question: loading modules at run time
by davorg (Chancellor) on Jan 23, 2001 at 19:15 UTC
    if ($we_want_to_use_lwp_simple) { require LWP::Simple; LWP::Simple->import; }
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: Related question: loading modules at run time
by goldclaw (Scribe) on Jan 23, 2001 at 19:30 UTC
    Undeclared functions at compile time is only a problem if you call them without paranteheses, that is
    my $result=function_one sub function_one{ #blah, blah }
    If you wish to use the functions in LWP that way, you will have to do something like this:
    eval q( use LWP::UserAgent qw(whatever); my $ua=LWP::UserAgent::new; );
    Ugh, ugly...probably better to try something like this:
    require LWP::UserAgent; import LWP::UserAgent(qw(whatever)); no strict subs; my $ua=LWP::UserAgent::New;
    If you can manage to put a pair of parantheses after each method call, this will do:
    require LWP::UserAgent; import LWP::UserAgent(qw(whatever)); my $ua=LWP::UserAgent::new(); #continue doing what you do with LWP...
    Notice that theres no need for a no strict subs when you tell perl that this is a function call.
    AUTOLOAD is used in packages for handling calls to routines within the package that are not defined in the package. One way to use this feature, is to do some custom error handling.
    In a program I'm writing at the moment, I use it to forward unknown method calls to a different object. Sort of a mix between is-a and has-a relationship.
    However, AUTOLOAD does not automatically load subroutines you need.
    GoldClaw
Re: Related question: loading modules at run time
by repson (Chaplain) on Jan 24, 2001 at 09:07 UTC
    The autouse module may do what you want, but unfortunatly the docs don't seem to go into sufficient detail on when the module is actually loaded into memory. But if it works like it seems like it should than it would be better than manually doing require and import or using a custom AUTOLOAD.

Log In?
Username:
Password:

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

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

    No recent polls found