Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Program structure: subs vs modules vs Selfloader

by Arunbear (Prior)
on Jun 20, 2004 at 14:18 UTC ( #368280=note: print w/replies, xml ) Need Help??

in reply to Program structure: subs vs modules vs Selfloader

Here's one solution:
# Give largesub and its entourage their own module package ModuleWithLargeSub; sub smallsub { ...process... } sub largesub { my ($e, $f, $g, $h) = @_; my $foo = &smallsub($e); ...process... return \@AoH; } 1; # Then in the cgi script: #!/usr/bin/perl use strict; my $run_large_sub; ...etc... ...initiate variables... ...HTML::Template setup... my $AoH; if($run_large_sub) { require ModuleWithLargeSub; $AoH = ModuleWithLargeSub::largesub($a, $b, $c, $d); } $template -> param( todisplay => $AoH ); print $template->output(); exit();
Also, serioulsy consider breaking the large sub into smaller ones!

Replies are listed 'Best First'.
Re^2: Program structure: subs vs modules vs Selfloader
by bradcathey (Prior) on Jun 20, 2004 at 17:31 UTC
    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:
    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.

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

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

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2020-11-25 06:08 GMT
Find Nodes?
    Voting Booth?

    No recent polls found