Zaxo,
One problem with Perl5's references is that there is no auto-dereferencing. Otherwise, you wouldn't have to resort to tying a variable
my $dbh = lazy_dbh( $dsn, $user, $pass, \%attr );
$dbh->()->ping();
sub lazy_dbh {
my @args = @_;
my $dbh;
return sub {
require DBI;
if ( ! defined $dbh || ! $dbh->ping() ) {
$dbh = DBI->connect( @args ) or die $DBI::errstr;
}
return $dbh;
};
}
It just gets to be a pain to use $dbh->() every where. You could also use my
Multi-Method closures technique, but you need to remember to put {} around the method names. There's also the issue of keeping the hash keys synced with
DBI's methods. You could tie the underlying hash so that it looked up each method on first invoke and kept it around for later. Then we are back to square one - tying tied variables.
I guess what I am saying is that I think this is a nice use of Tie::Constrained, but that there is no such thing as a free lunch.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.