Hey Eric --
> I wanted the DBH to be global since just about every sub in Holds does a
> query of some sort. I guess it doesn't matter either way if I do
> the connect
> in the new() vs up top outside of a sub.
CGI::Application has a facility which is intended to solve exactly this type
of problem -- the param() method. The param() method allows you to stash a
property (such as a $dbh) in your CGI::Application-based object which can be
retrieved anywhere. I typically connect to the database in my setup()
method and stash my $dbh for use later:
package My::WebApp;
use strict;
use base qw/CGI::Application/;
sub setup {
my $self = shift;
$self->start_mode('start');
$self->run_modes(
'start' => 'run_mode_method'
);
my $dbh = $self->connect_to_database();
$self->param('DBH', $dbh);
}
sub run_mode_method {
my $self = shift;
# Get database handle
my $dbh = $self->param('DBH');
my $output = '';
# ...etc....
return $output;
}
Furthermore, in order to disconnect, the teardown() method may be used:
sub teardown {
my $self = shift;
# Get database handle
my $dbh = $self->param('DBH');
$dbh->disconnect();
}
Refer to the CGI::Application POD for details on teardown() and param().
Regarding connecting to the database, I also urge you to encapsulate your
connection code. On my projects I always get things started by creating a
Perl module which I use whenever I need a database connection:
package My::DatabaseCreds;
use DBI;
sub new_dbh {
my $dbh = DBI->connect(....)
die ("Can't connect: ".$DBI::errstr) unless ($dbh);
return $dbh;
}
(This isn't exactly the code I use -- I actually have a module which I
sub-class, but you get the idea.)
The benefit of creating a module is that (1) all your Perl code can use this
module so that (2) should it ever have to change you can change it in one
place.
> What is the problem with the my $holdcheck = new Holds() type of syntax?
> I never read anything about that either way.
My guess is that Perrin was referring to your use of the "indirect"
notation, as opposed to the "arrow" notation:
Indirect:
my $holdcheck = new Holds()
Arrow:
my $holdcheck = Holds->new()
Many people (myself included) prefer the arrow notation. In general, the
arrow notation tends to be less ambiguous, particularly when it comes to
combining method calls with arguments.
HTH,
-Jesse-
--
Jesse Erlbaum
The Erlbaum Group
[EMAIL PROTECTED]
Phone: 212-684-6161
Fax: 212-684-6226