Hi,
I only vaugely understand that using new thingy() can lead to confusion
about which package the sub is suppost to be in.
But I have changed all of these in my code. So my $checkhold = Holds->()
now. This is kind of tricky I think, because I am so used to the new in
Java, but I seem to remember people claiming that wasn't really right even
in java were the new() means something more.
I also made a small module to do the DB connect and used it in my other
modules. I will use your suggestion in my CGI::App module, but most of my
other modules need a db connection too.
Another clue to my problem is that it seems like some commits(mysql Innodb
4.02) were not getting done even though I explictly did commit and checked
for errors from the commit. So now some updates are being done with
AUTOCOMMIT=1
I had thought about using tear down as a final check to make sure commits
had been done, but I don't know how to do that and use Apache::DBI at the
same time.
Thanks,
Eric
At 03:25 PM 10/14/02 -0400, Jesse Erlbaum wrote:
>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
> suppressed
> Phone: 212-684-6161
> Fax: 212-684-6226
>
>
(250) 655 - 9513 (PST Time Zone)
"Inquiry is fatal to certainty." -- Will Durant
---------------------------------------------------------------------
Web Archive: http://www.mail-archive.com/suppressed/
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed
Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.