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; }