in reply to A Lazy Class

I'd use tie() instead. This module will untie the variable and replace it with its value the first time the variable is fetched.
package Tie::Scalar::Lazy; # usage: # use Tie::Scalar::Lazy; # lazy $value => \&expensive_function; require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( lazy ); sub lazy { tie $_[0], __PACKAGE__, \$_[0], $_[1]; } sub TIESCALAR { my ($class, $sref, $cref) = @_; bless [ $sref, $cref ], $class; } sub FETCH { my $self = shift; my $value = $self->[1]->(); untie ${ $self->[0] }; ${ $self->[0] } = $value; } 1;

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: A Lazy Class
by chip (Curate) on Apr 22, 2002 at 02:45 UTC
    That's a gutsy play, japhy. I wonder if anyone has ever tested untying a value inside a FETCH method on that selfsame value? If not, I'd run the test under a memory leak detector. It wouldn't be hard for Perl to leak memory due to an unexpected circular dependency.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      I've done it before, and I'll do it again!

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;