I can manually call $tied_scalar->FETCH on the tied scalar and it works, but just $tied_scalar does not automatically call fetch.
I've only skimmed through your long post, but I do get the strong impression you're misunderstanding how tieing works. You see, with a tied variable, you have two, er, "variables": the tied variable, and the object. The object is associated to the tied variable, but only behind the scenes, you don't see it.

Normally, you don't touch the object, except inside the access methods, like FETCH. If you try to access the tied variable with common perl syntax, perl uses the object to drive how it behaves.

Got it? The object is not tied.

use My::Scalar; tie my $x, 'My:Scalar'; my $tieobj = tied $x;
Whenever you do anything to $x, a method for $tieobj is called internally. You normally don't use $tieobj in plain code.

Stop messing with $tieobj, it's $x you should be using.

And yes, if you need it, you can tie a hash item. This should work:

tie $hash{foo}, 'My:Scalar';

p.s. And stop using __PACKAGE__ to bless your objects. TIESCALAR is a constructor (a class method), and thus it is called with the class name as the first parameter, not with an object as you seem to be assuming. So your TIESCALAR should look like this instead:

sub TIESCALAR { my ($class, $key, $val) = @_; return bless {'value' => $val, 'key' => $key , 'fetched' => 0 }, $class; }

In reply to Re: A Tied hash returning a tied scalar which becomes untied, why? by bart
in thread A Tied hash returning a tied scalar which becomes untied, why? by Withigo

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.