I am trying to make a tied scalar, that on its first FETCH, will turn itself back into a normal untied scalar and then set itself to the result of a sub in its parent object where the tied/untied scalar lives as a hash slice. The point is, the value has to be generated just once, only the first time. No point in keeping it tied for the rest of the life of myObj object. Its an attempt at efficiency. The weaken is to prevent a memleak/circular reference. mytie class MUST obtain the permanent value of the tied scalar from myObj/its parent.

The reason I am using a tied scalar verse a GetValue() method on myObj is, myObj is a large complicated object with inheritance and everything. Its less bug inducing to make $self->{'tiedScalar'} a tied scalar for a short while, than change hundreds of places where $self->{'tiedScalar'} is accessed among multiple modules.

The problem is, that tied() doesn't work inside the FETCH method. untie() does work, but it never calls UNTIE method.
#!/usr/bin/perl -w package mytie; use Scalar::Util qw( weaken ); sub TIESCALAR { print "TIESCALAR\n"; my $class = shift; my $self = {'myiobj' => shift}; weaken($self->{'myiobj'}); return bless($self, $class); } sub FETCH { print "FETCH\n"; my $obj = $_[0]->{'myiobj'}; print "going to untie\n"; print "tied() is ".tied($obj->{'tiedScalar'})."\n"; untie($obj->{'tiedScalar'}); print "tied() is ".tied($obj->{'tiedScalar'})."\n"; print "untied\n"; $obj->{'tiedScalar'} = $obj->GetFinalValue(); } sub STORE { print "STORE\n"; } sub DESTROY { print "DESTROY\n"; } sub UNTIE { print "UNTIE\n"; } package myObj; sub new { bless({'tiedScalar'=> undef, 'finalValScalar' => "final value\n"} +); } sub tiescalar { my $self = shift; tie($self->{'tiedScalar'}, 'mytie', $self); } sub untiescalar { my $self = shift; untie($self->{'tiedScalar'}); } sub GetFinalValue { my $self = shift; return $self->{'finalValScalar'}; } sub DESTROY { print "myObj destroyed\n"; } package main; { my $o = myObj::new(); $o->tiescalar(); #comment one and uncomment the other below print '$o->{\'tiedScalar\'} is '.$o->{'tiedScalar'}; #$o->untiescalar(); print tied($o->{'tiedScalar'})?"tiedScalar is still tied\n":"tied +Scalar is now untied\n"; } 0;
with "print '$o->{\'tiedScalar\'} is '.$o->{'tiedScalar'};" I get
C:\Documents and Settings\Owner\Desktop>perl n8.pl TIESCALAR FETCH going to untie Use of uninitialized value in concatenation (.) or string at n8.pl lin +e 19. tied() is DESTROY Use of uninitialized value in concatenation (.) or string at n8.pl lin +e 21. tied() is untied $o->{'tiedScalar'} is final value tiedScalar is now untied myObj destroyed C:\Documents and Settings\Owner\Desktop>
with "$o->untiescalar();" I get
C:\Documents and Settings\Owner\Desktop>perl n8.pl TIESCALAR UNTIE DESTROY tiedScalar is now untied myObj destroyed C:\Documents and Settings\Owner\Desktop>
I am using ActivePerl 5.10.0. So whats going on here? Is this a bug in perl or some attempt perl is making at preventing recursion?

In reply to untie() and tied() and UNTIE dont work right inside FETCH with tied scalar by patcat88

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.