Hi,
although that's more perl than mod_perl related I'd do the following:
1) make Perl-Modules out of e.g. mysql.pl e.g. MyModule::MySQLSupport
2) load them using use MyModule::MySQLSupport in webapp.pl which should
also be a Perl-Module under mod_perl e.g. MyModule::WebApp
3) When you are using Perl-Modules you can easily import variables from
MyModule::MySQLSupport into MyModule::WebApp using Exporter see
Chapter 11 in "Programming Perl".
For future Perl-Coding I'd suggest that you avoid using globals and
maybe use an object-oriented approach, using anywhere global variables
is one of the biggest design flaws I can think of. I can only think
about a handful of situations where I'd use globals.
Tom
On Wed, 2004-02-11 at 03:13, ian douglas wrote:
> Hey all,
>
> Poured over a number of texts online and having trouble easily converting a mass
> quantity of existing code to work well under mod_perl in an easy-to-manage
> method.
>
> Currently, we have a few MB of unique Perl scripts running various web
> applications, and I'm having trouble scoping when requiring those scripts in our
> main application.
>
> We have a 'base' script, which I'll call webapp.pl, which requires a number of
> other scripts, including a script that loads up a number of cursors for our
> database at startup so they don't get prepared over and over and over. The
> scoping of the cursor names is giving me grief though:
>
> webapp.pl
> ---
> #!/usr/bin/perl
> use strict;
> use vars qw(
> $dbh
> $cursor1
> $cursor2
> $cursor3
> etc
> $cursor250
> ) ;
>
> require ("mysql.pl") ; ## has sub's to connect/disconnect to mysql
> require ("cursors.pl") ; ## example printed below
>
> &connect_to_mysql() ; ## defined in mysql.pl
> &declare_cursors() ; ## defined in cursors.pl (see below)
>
> # do work here
>
> &finish_cursors() ; ## defined in cursors.pl (see below)
> &disconnect_from_mysql() ; ## defined in mysql.pl
> exit ;
>
>
> cursors.pl
> ---
> use vars qw(
> $dbh
> $cursor1
> $cursor2
> $cursor3
> etc
> ad nauseum
> $cursor250
> ) ;
>
> sub declare_cursors
> {
> $cursor1 = $dbh->prepare ("SELECT something FROM something WHERE something=?")
> ;
> etc
> $cursor250 = ...
> }
> sub finish_cursors
> {
> $cursor1->finish ;
> etc
> }
>
>
>
> The problem I'm having is that the 'use vars' from the top of cursors.pl doesn't
> give webapp.pl scope of its variables, so I have to redefine the entire list of
> cursors in all of the scripts we have if the script uses any of the cursors
> defined in cursors.pl, which is a nuisance if I need to remove or edit a
> cursor's name... I end up with dozens of places to change the cursor name or
> remove a cursor.
>
> Is there an easier way to manage this?
>
> It's not just with this cursors.pl script either - we'd like to define other
> Perl scripts with their own global variables that can build those variables for
> global scope simple by requiring the script, but haven't seen anything yet to
> accomplish this.
>
> Another example would be for webapp.pl to require our "cookies.pl" code that
> looks like this:
>
> cookies.pl
> -----
> use vars qw(
> %COOKIES
> ) ;
>
> sub getallcookies()
> {
> my %mycookies ;
> # etc etc, parse what the browser gives us
> return %mycookies ;
> }
>
>
> In order to make this call within webapp.pl:
>
> %COOKIES = &getallcookies() ;
>
> ... we have to define %COOKIES all over again.
>
>
>
> Thanks for any assistance.
>
> Ian Douglas
--
\\\||///
\\ - - //
( @ @ )
-oOo--( )--oOo----------------------------------------------------------
___ ___ tom schindl
o __ / / / innovative medientechnik planung AG
/ /\/\/ / / /__/ / __ mailto:[EMAIL PROTECTED]
/ / / / /_/ / / /___/ http://www.impire.de
/ voice:+43(512)34193431,fax:+43(512)34193420
Eduard-Bodem-Gasse 6, A-6020 Innsbruck, Austria, Software Engineering
------------------------------------------------------------------------
--
Reporting bugs: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html