2009/11/18 Randal L. Schwartz <mer...@stonehenge.com>: > > I prefer a pattern that looks more like this: > > sub expensive_to_calculate { > > my $self = shift; > my $input = shift; # cache on this scalar > > our %cache_for_expensive_to_calculate; > return $cache_for_expensive_to_calculate{$input} ||= do { > expensive calculation here; > more things based on $input; > last expression is value; > }; > > } > > Yes, in 5.10, this can be simplified, but I find 5.8 to still be > the dominant Perl. > > This also presumes that a "false" value is never needed to be cached.
Hi, Re-hashing an old thread here. I've tried to borrow the above pattern for something I'm doing but I'm wondering if this is going to work in the environment I working in. It' is perl, v5.6.2, on a modperl 1, and the although the package is a class, somehow the package/file I'm working with is being used in a function-orientated way. Yes, it all needs updating but I need to get this last feature in before I move it to new architecture. sub get_prices_from_code { my $code = shift; our %cache; return $cache{$code} ||= do { my $sql = qq(SELECT DISTINCT price_code, price_id FROM prices); my $dbq = dbiQuery($sql); if ($dbq->success && $dbq->rows) { while ( $dbq->fetch() ) { if ($code eq ($dbq->row())[0] ) { $code = ($dbq->row())[0]; $cache{$code} = ($dbq->row())[1]; } } return $cache{$code}; } } } I guess the question is: Will the declaration of our %cache within a function-orientated sub-routine render it always uninitialized? Thanx, Dp. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/