in reply to Best practices passing database handles, cgi objects, etc.

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

Replies are listed 'Best First'.
Re^2: Best practices passing database handles, cgi objects, etc.
by xtpu2 (Acolyte) on Feb 19, 2014 at 00:19 UTC

    If I understand the example correctly, then the main program (the one calling MyApp) will look something like this?

    use MyApp; my $myapp->new( cgi => CGI->new(), session => CGI::Session->new(), database => DBI->connect() ); print CGI->header().$myapp->do_something();

      Yes, or better:

      use MyApp; my $myapp->new( cgi => CGI->new(), session => CGI::Session->new(), database => DBI->connect() ); print $myapp->process_request();

      Where the process_request method is something like:

      sub process_request { my $self = shift; $self->cgi->header . $self->do_something; }
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      I usually include all the functionality into the class so I do not have to care about its implementation in the main application. Therefore, I would use
      $myapp->header

      instead of

      CGI->header

      If you later decide you need to change something in the header (e.g. the encoding/charset), you just edit the header method of the class.

      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: Best practices passing database handles, cgi objects, etc.
by McA (Priest) on Feb 18, 2014 at 14:51 UTC

    I really hoped that one of our Abbots or other master monks would come out of their meditation to enlighten us. Special thanks for posting the addendum ;-)

    I wish I could see more comments on this topic.

    Best regards
    McA