Hmmm ... strange. That doesn't seem to work. I tried the additional parameter, I even tried to add a ref to the tied variable to the object later and it did not work. The variable appeared not to be tied within FETCH and stayed tied outside:

---------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;
and it prints
ref=SCALAR(0x1a4587c) Tied: Data::Lazy=HASH(0x15d55d0) Try to untie SCALAR(0x1a4587c) tied to $x=2 Tied: Data::Lazy=HASH(0x15d55d0) $x=2
That is I do have a reference to the right scalar, yet it's not tied ?!?

I use Perl 5.8 (ActivePerl build 805) under Win2000serverSP4.

I also tried the same code using Perl 5.6.1 (ActivePerl build 631, same OS) and there it also did not report the reference to be tied, but did succeed to untie it:

ref=SCALAR(0x1ab2cc0) Tied: Data::Lazy=HASH(0x1abf078) Try to untie SCALAR(0x1ab2cc0) tied to $x=2 Tied: $x=2
Strange.

Jenda
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
   -- Rick Osborne

Edit by castaway: Closed small tag in signature


In reply to Re: Re: How to untie oneself? by Jenda
in thread How to untie oneself? by Jenda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.