Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

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


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

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.

  • Comment on Re^4: Does there exist a CPAN module for lazily initialized variables?
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2024-04-18 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found