punkish has asked for the wisdom of the Perl Monks concerning the following question:
I am in the process of cleaning up some crud I wrote -- taking a large web-app script and breaking it up into modules, one for each "component" (or 'run-mode') of the app. I have the following layout --
use CGI related modules; use HTML::Template; use DBI; use Myapp::Authenticate qw(authenticate); use Myapp::View qw(view); use Myapp::Update qw(update); # # you get the picture
There are many ways I can schlep the variables around.
# 1. create objects in the main script and pass them around my $cgi = new CGI::Simple; # unpack $cgi minimally to determine what to do my $action = $cgi->param('action'); my $dbh = DBI->connect(dbi stuff); my $template = HTML::Template->new(logic to determine template); # dispatch if ($action eq 'view') { view($cgi, $dbh, $template); } elsif ($action eq 'save') { save($cgi, $dbh, $template); } yadda yadda # then unpack the objects in the respective modules as needed #-------------------------------------- # 2. don't schlep the objects, but refer to them via fully # qualified names as needed # # create objects as above. unpack $cgi minimally to determine action # dispatch if ($action eq 'view') { view(); } elsif ($action eq 'save') { save(); } yadda yadda # then, in my modules package Myapp::View; sub view { my $target = $main::cgi->param('target'); my $this = $main::cgi->param('this'); my $that = $main::cgi->param('that'); my $sth = $main::dbh->prepare(...); $sth->execute; $main::template->param(RESULTS => $sth->fetchall_arrayref({}),); } #-------------------------------------- # 3. another variation # besides any mix of the above, don't import/export anything. # Refer to everything using their long names.
Since Perl gives me so many ways to hang myself, are there any caveats with any of the above, with respect to platforms, operating systems, long-term code maintenance, best-practices, etc.?
Or, am I just agonizing needlessly and should just jump in and do what I fancy today.
Is it just 6 of one and sqrt(36) of another?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing variables between packages
by Arunbear (Prior) on Aug 22, 2005 at 16:02 UTC | |
by bibliophile (Prior) on Aug 23, 2005 at 18:51 UTC | |
|
Re: Passing variables between packages
by merlyn (Sage) on Aug 22, 2005 at 15:58 UTC | |
|
Re: Passing variables between packages
by JediWizard (Deacon) on Aug 22, 2005 at 16:18 UTC |