Up Prev Next
From: Michael Peppler <mpeppler at MBAY dot NET>
Subject: Re: Passing a $dbh to a subroutine
Date: Sep 18 1997 4:50PM
Java G wrote:
> email@example.com wrote:
> > Can this be done? Right now in a majority of my applications I
> > write
> > subs to carry out inserts, updates, selects, etc.. Each one of
> > these
> > subs opens/keeps its own $dbh that is closed automatically when
> > the
> > sub exits. In order to avoid the overhead of opening/closing the
> > connection and re-specifying the connection params each time, can
> > I
> > just pass the $dbh to the sub and access it that way?
> > TIA,
> > Sal
> by default, perl variables are global, do there's no
> need to pass anything.
But unless you are writing small/trivial scripts I would strongly
recommend that you always use the 'strict' pragma and the -w
switch. You'll avoid tons of typos and other simple mistakes.
These two options will force you to pre-declare global variables
(via the use vars() pragma) or use fully qualified variable names
(like $main::dbh), or use lexical variables (my $dbh).
This is a GOOD THING to keep your programs as clean and easy to
debug (and enhance) as possible.
Michael Peppler -||- Data Migrations Inc.
firstname.lastname@example.org -||- http://www.mbay.net/~mpeppler