[ABE.pm] And another thing...

Ricardo SIGNES rjbs-perl-abe at lists.manxome.org
Mon May 22 22:02:04 PDT 2006


* "Faber J. Fedor" <faber at linuxnj.com> [2006-05-23T00:49:43]
> 
> > What I'm wondering is why it's not better to say:
> > 
> >   my $sth = $self->dbh->prepare(...);
> > 
> > ...where the dbh method is the get_dbh method with a few fewer chars.  
> 
> How do you get from get_dbh() to 'my $sth = $self->dbh' or even 'my $sth
> = $self->{dbh}'? You dropped a 'get_' in there!

Instead of this:

  my $dbh;
  sub get_dbh {
    return $dbh if $dbh;
    $dbh = DBI->connect(...);
    return $dbh;
  }

...I write exactly the same thing, but with a different name for the sub.

In reality, though, I'd write something more like:

  my $dbh;
  sub dbh { $dbh ||= DBI->connect(...); }

> Most of my code has been
> 
>     my @returnedRow = $self->{"_DBH"}->selectrow_array($stmt);

As a general rule, only one method should ever access any given hash entry on
your object, if it's a blessed hash.  Everything else should use that method.
So, getting at the dbh associated with the object should be done with the "dbh"
method, which is the only thing that looks at $self->{_DBH}

There are exceptions, but that's a good starting point.  It makes future
extensibility much, much, much simpler.  I have spent countless hours fixing
code that doesn't follow this guideline.

-- 
rjbs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20060523/683d4b8b/attachment.bin 


More information about the ABE-pm mailing list