Use object-oriented code. This will allow you to bundle up a few separate variables into a single object.

package MyApp { use Moo; has cgi => (is => 'ro'); has database => (is => 'ro'); has session => (is => 'ro'); sub do_something { my $self = shift; $self->databsse->foo(...); $self->cgi->bar(...); my $foo = $self->another_subroutine($somevar, $othervar); return $self->final_result($foo); } sub another_subroutine { my $self = shift; my ($somevar, $othervar) = @_; return $self->database->baz($self->cgi, $somevar, $othervar); } sub final_result { my $self = shift; my ($foo) = @_; my $result = $self->cgi + $self->session + $foo; return $result; } }

Any sufficiently complex application will need more than one class though, so there will still be an element of passing handles around when one object needs to construct another object. But this can be made quite neat and self-contained. For example, let's assume that MyApp objects occasionally need to create MyApp::Article objects, representing a page of content...

package MyApp { ...; # all that stuff above has article_class => (is => 'ro', default => 'MyApp::Article'); sub create_article_object { my $self = shift; return $self->article_class->new( cgi => $self->cgi, database => $self->database, session => $self->session, @_ ); } sub get_home_page { my $self = shift; return $self->create_article_object(identifier => 1); } sub get_contact_page { my $self = shift; return $self->create_article_object(identifier => 2); } } package MyApp::Article { use Moo; has identifier => (is => 'ro'); has cgi => (is => 'ro'); has database => (is => 'ro'); has session => (is => 'ro'); ...; }

That's all object-oriented programming is really... packaging up bundles of subs into neat little objects that contain all the data those subs need.

Aside: you can do the exact same thing with closures, but in Perl objects are somewhat more elegant than closures.

The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."

Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.

On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.

Guy Steele

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re: Best practices passing database handles, cgi objects, etc. by tobyink
in thread 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.