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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |