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.
Whenever you do anything to $x, a method for $tieobj is called internally. You normally don't use $tieobj in plain code.use My::Scalar; tie my $x, 'My:Scalar'; my $tieobj = tied $x;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |