---------testUntie.pl--------- #!perl use Data::Lazy; print "ref=" . \$x . "\n"; tie $x, 'Data::Lazy', sub {sleep 1; 2}; tied($x)->{'var'} = \$x; print "Tied: " . tied($x) . "\n"; print "\$x=$x\n"; print "Tied: " . tied($x) . "\n"; print "\$x=$x\n"; ---------Data/Lazy.pm--------- package Data::Lazy; #... sub TIESCALAR { my $pack = shift; my $self = {}; $self->{code} = shift; $self->{'store'} = $_[0] if $_[0]; $self->{'type'} = 0; # $self->{'var'} = $_[1] if $_[1]; bless $self => $pack; # That's it? Yup! } #... sub FETCH { my $self = shift; if ($self->{'type'} == 0) { # scalar return $self->{value} if exists $self->{value}; if (ref $self->{code} eq 'CODE') { $self->{value} = &{$self->{code}}; } else { $self->{value} = eval $self->{code}; } if ($self->{'store'} == LAZY_STOREVALUE and exists $self->{'var'}) { my $var = $self->{'var'}; print "Try to untie $var tied to " . tied($$var) . "\n"; untie($$var); $$var = $self->{value}; undef $self; return $$var; } else { $self->{value}; } } # ... elsif array and hash } #... 1;