Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Program structure: subs vs modules vs Selfloader

by bradcathey (Prior)
on Jun 20, 2004 at 17:31 UTC ( #368308=note: print w/replies, xml ) Need Help??


in reply to Re: Program structure: subs vs modules vs Selfloader
in thread Program structure: subs vs modules vs Selfloader

Arunbear, thanks for the example. However, if I'm permitted, I have a follow-up question: does coding it the way you do, by "requiring" the module only if the condition is met, only load the module when it runs?

Normally I reference the module like this:
use SomeModule.pm
at the start of my program, right after the shebang. Thanks.

Update: Well, I Googled and found this informative piece on modules, libraries, etc. I see now that Perl doesn't deal with a require until it comes across it in the code.

—Brad
"Don't ever take a fence down until you know the reason it was put up. " G. K. Chesterton

Replies are listed 'Best First'.
Re^3: Program structure: subs vs modules vs Selfloader
by vek (Prior) on Jun 20, 2004 at 18:31 UTC

    If you use a module, Perl deals with it at compile time. If you require a module, Perl deals with it at run time.

    I typically like to use a module because it allows me to check everything via a perl -c someprog.pl after I've finished coding. It I used require instead, I wouldn't find out that something was wrong until I ran the program for the first time. YMMV, just my own personal preference.

    require is handy though if your program needs to do something based on whether you have a module installed on your system. You can test for this at runtime.

    #!/usr/bin/perl -w use strict; # do we have FOO::Bar on this system? eval { require FOO::Bar }; if ($@) { # FOO::Bar not installed } else { # FOO::Bar installed }
    -- vek --

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-11-27 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?