Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Passing variables between packages

by punkish (Priest)
on Aug 22, 2005 at 14:40 UTC ( #485699=perlquestion: print w/replies, xml ) Need Help??

punkish has asked for the wisdom of the Perl Monks concerning the following question:

YASRQ (yet another style related question)

I am in the process of cleaning up some crud I wrote -- taking a large web-app script and breaking it up into modules, one for each "component" (or 'run-mode') of the app. I have the following layout --

use CGI related modules; use HTML::Template; use DBI; use Myapp::Authenticate qw(authenticate); use Myapp::View qw(view); use Myapp::Update qw(update); # # you get the picture

There are many ways I can schlep the variables around.

# 1. create objects in the main script and pass them around my $cgi = new CGI::Simple; # unpack $cgi minimally to determine what to do my $action = $cgi->param('action'); my $dbh = DBI->connect(dbi stuff); my $template = HTML::Template->new(logic to determine template); # dispatch if ($action eq 'view') { view($cgi, $dbh, $template); } elsif ($action eq 'save') { save($cgi, $dbh, $template); } yadda yadda # then unpack the objects in the respective modules as needed #-------------------------------------- # 2. don't schlep the objects, but refer to them via fully # qualified names as needed # # create objects as above. unpack $cgi minimally to determine action # dispatch if ($action eq 'view') { view(); } elsif ($action eq 'save') { save(); } yadda yadda # then, in my modules package Myapp::View; sub view { my $target = $main::cgi->param('target'); my $this = $main::cgi->param('this'); my $that = $main::cgi->param('that'); my $sth = $main::dbh->prepare(...); $sth->execute; $main::template->param(RESULTS => $sth->fetchall_arrayref({}),); } #-------------------------------------- # 3. another variation # besides any mix of the above, don't import/export anything. # Refer to everything using their long names.

Since Perl gives me so many ways to hang myself, are there any caveats with any of the above, with respect to platforms, operating systems, long-term code maintenance, best-practices, etc.?

Or, am I just agonizing needlessly and should just jump in and do what I fancy today.

Is it just 6 of one and sqrt(36) of another?

--

when small people start casting long shadows, it is time to go to bed

Replies are listed 'Best First'.
Re: Passing variables between packages
by Arunbear (Prior) on Aug 22, 2005 at 16:02 UTC
    Please look at CGI::Application (and also CGI::Application::Dispatch, once you've digested the former). The documentation is very readable and includes examples which you will find illuminating.

    I don't want to spoil the plot for you but here's a snippet - using this approach your view runmode may look like:

    sub view { my $self = shift; my $q = $self->query; my $dbh = $self->param('dbh'); ... }
    C::A objects can get to the cgi object via $self->query, so no need to pass that around. And you can pass other stuff between runmodes using C::A's param method.

      I wish I could double-plus this!

      It's well worth the (slight) time and effort to get comfortable with CGI::Application.

      Soon you, too, can be mad-drunk with power! Er... I mean... "Able to crank out complex and powerful web apps in your sleep".

Re: Passing variables between packages
by merlyn (Sage) on Aug 22, 2005 at 15:58 UTC
    If you're passing the globals around because they really are singletons in your app, then make wrapper singletons. In your main:
    MyDatabase->configure(...); MyCGI->configure(...); MyTemplate->configure(...); ... my $action = MyCGI->get_param('action'); if ($action eq 'start') { view('start'); } elsif ($action eq 'finish') { view('finish'); }
    And so on. Configure your singletons, then let your modules also use those same singletons.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Passing variables between packages
by JediWizard (Deacon) on Aug 22, 2005 at 16:18 UTC

    This may already be covered in merlyn's reply... but IMHO: You should never explicitly refer to main::anything from with in a package (other than main). The same goes for all packages. ASAIK The point of making a packge it to hide data/functions/whatever form other packages. If you need to maniplate vailables in another package it should either have an accessor method, or export the variable into your namespace. That is just a "Best Practice"... again My Humble Oppinion.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://485699]
Approved by phaylon
Front-paged by xorl
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-04-11 19:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?