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


In reply to (bbfu) (proxy object) Re: tying tied hash scalars by bbfu
in thread tying tied hash scalars by steves

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.