I'm working on a somewhat complex application that uses CGI, CGI::Session and DBI. I've been following a practice of having no globals and passing variables to every subroutine that I have because this served me well in the past. However, I'm running into the problem that I'm passing 5-6 variables to each subroutine, and what's worse, sometimes that particular subroutine doesn't even need the variables, except to pass to another subroutine (!).</p.

So for example:

MAIN: { my $cgi = CGI->new(); my $session = CGI::Session->new(...); my $dbh = $dbh->connect(...); my $result = do_something($cgi, $session, $dbh); print $cgi->header().$result; } sub do_something { my ($cgi, $session, $dbh) = @_; $dbh->foo(...); $cgi->bar(...); my $foo = another_subroutine($cgi, $dbh, $somevar, $othervar); return final_result ($cgi, $session, $foo); } sub another_subroutine { my ($cgi, $dbh, $somevar, $othervar) = @_; return $dbh->baz($cgi, $somevar, $othervar); } sub final_result { my ($cgi, $session, $foo) = @_; my $result = # operations with $cgi, $session and $foo return $result; }

Basically, you get the idea. I'm probably doing something wrong when it comes to the actual flow of the program, but I have no idea what or where to start. Maybe what I'm really asking for are tips on designing perl CGI applications?


In reply to Best practices passing database handles, cgi objects, etc. by xtpu2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.