Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Does there exist a CPAN module for lazily initialized variables?

by jryan (Vicar)
on Jul 14, 2004 at 19:08 UTC ( [id://374398]=note: print w/replies, xml ) Need Help??


in reply to Re: Does there exist a CPAN module for lazily initialized variables?
in thread Does there exist a CPAN module for lazily initialized variables?

Right, that's the thing; I have code that's essentially just like that all over the damn place. I'm trying to refactor/clean it up right now.
  • Comment on Re^2: Does there exist a CPAN module for lazily initialized variables?

Replies are listed 'Best First'.
Re^3: Does there exist a CPAN module for lazily initialized variables?
by dragonchild (Archbishop) on Jul 14, 2004 at 19:15 UTC
    sub build_lazy { my ($var, $callback, @params) = @_; eval <<__EVAL__; { my \$$var; sub $var { \\\$var = $callback->(@params) unless defined \$$var; \$$var; }; } __EVAL__ } # Later ... build_lazy( 'dbh', \&build_dbh, @dbh_params ); build_lazy( 'foo', \&build_foo );

    Of course, you might want some dwimmery for hashes vs. arrays vs. scalars, unless you're ok with everything as a reference. I would be, but that's just me. And, you might want to throw BEGIN or CHECK around the calls to build_lazy(), but that's probably overkill.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Well, if we're going with the sub approach, the eval isn't needed:

      use strict; sub build_lazy { my ($name, $callback, @params) = @_; my $var; no strict 'refs'; *$name = sub { $var = $callback->(@params) unless defined $var; $var; }; } build_lazy( 'dbh', sub { print @_ }, 'hi'); print dbh(), dbh(); # hi11

      Its not that I don't like this approach (its still a hell of a lot better than what I have now), I just feel that... I don't know, just that its not the be can be done. Then again, I guess I have a weird mental condition where I'm unsatisfied with my code unless it takes on *exactly* the form thats in my mind, and I become uncomfortable when it isn't. I'm kind of weird. Thanks for the idea though.

Re^3: Does there exist a CPAN module for lazily initialized variables?
by diotalevi (Canon) on Jul 14, 2004 at 19:11 UTC
    I really like dbh() because it gives you a runtime chance to automatically reconnect or refresh or whatever as needed which you wouldn't get if you just accessed a global $DBH.

      Yeah, my little class in the root node would do the same thing, since it just checks for definedness. I just want a pretty tied interface to it. I guess I'll just write my own class, and maybe make it general enough for CPAN...

        No, the difference here is that you make this call $dbh->connect_cached() so that instead of just storing your object - you revivify it automatically as needed. I think of this as important for something that will be long running - I'd just go with a plain stored value otherwise.

        sub main::dbh { # for daemons and long running code - reconnect as needed DBI->connect_cached( ... ) } sub main::dbh { # initialize the connection or re-use. $::DBH ||= DBI->connect( ... ) }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://374398]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 02:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found