Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

Cheers - L~R


In reply to Re: Lazy DBI Connection Handles by Limbic~Region
in thread Lazy DBI Connection Handles by Zaxo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found