in reply to tying tied hash scalars

The best way I can think of (as in, requiring the least amount of existing codebase changes) is to create a "proxy object". In other words, tie the original hash to your own class which looks for the key you want to have tied separately and handles it specially.

#!/usr/bin/perl -l use warnings; package Tie::MyHash; use Tie::Hash; @ISA = ('Tie::StdHash'); use Tie::IxHash; use Tie::Scalar::Timeout; sub TIEHASH { my $class = shift; my $hash = {}; tie %$hash, 'Tie::IxHash'; tie ${$hash->{'special'}}, 'Tie::Scalar::Timeout', EXPIRES => '+ +2s'; return bless $hash, $class; } sub FETCH { my ($this, $key) = @_; if($key eq 'special') { print tied(${$this->{'special'}}) if $key eq 'special'; return ${$this->{'special'}}; } else { print tied(%$this); return $this->{$key}; } } sub STORE { my ($this, $key, $value) = @_; return ${$this->{'special'}} = $value if($key eq 'special'); return $this->{$key} = $value; } sub AUTOLOAD { my $this = shift; my $sub = $AUTOLOAD; $sub =~ s/.*:://; tied(%$this)->$sub(@_); } # Tie::IxHash doesn't have a DESTROY method, so we need one sub DESTROY {} package main; tie my %hash, 'Tie::MyHash'; $hash{'mundane'} = 'foo'; $hash{'special'} = 'bar'; print $hash{'mundane'}; print $hash{'special'}; print "Keys: ", join(", ", tied(%hash)->Keys(0, 1)); sleep 3; print $hash{'special'} || 'undef';

Update: Hrm. I just realized that the "special" key will not be considered part of the Tie::IxHash at all, for ordering purposes and such, so this isn't really a good solution either. I'll have to think about that...

Update2: It was straight-forward to change it so that the "special" key was stored in the Tie::IxHash. Updated the code accordingly.

bbfu
Black flowers blossum
Fearless on my breath

Replies are listed 'Best First'.
Re: (bbfu) (proxy object) Re: tying tied hash scalars
by steves (Curate) on Dec 02, 2002 at 21:49 UTC

    I am using my own ties and I tried several kinds of trickery in there. No luck.

    It turns out, for this particual problem, I don't need this. I reanalyzed the design and found I was anticipating a need that will never really exist. But I'd still like to know the general approach here and what Perl is supposed to support. I like using ties but I've been caught before assuming I can do too much with them.

    Somewhat related: For another package I'm considering, I'd also like to be able to tie a file handle more than once; i.e., provide multiple file handle filters without anyone knowing. The only approach I can see there is to somehow maintain a list of ties ... If anyone's done anything here I'd like to hear about your approach.