An alternative would be to create a tied hash class that has the bahavior you want, so that if you look up name, and there is no key name but there is one called redirect_name, you get the value associated with redirect_name transparently. Perhaps something like this:

package Redirect_Hash; sub TIEHASH { my ($class, $prefix) = @_; $prefix = 'redirect_' unless defined $prefix; my $self = [$prefix, {}]; bless $self => $class; } sub STORE { my ($self, $key, $value) = @_; my ($prefix, $hash) = @$self; if (index($key, $prefix) == 0) { my $truncated = substr($key, length($prefix)); $hash->{$truncated} = $value unless exists $hash->{$truncated}; } $hash->{$key} = $value; } sub FETCH { my ($self, $key) = @_; my ($prefix, $hash) = @$self; $hash->{$key}; }

To use this, you'd say:

tie %h => Redirect_Hash; $h{redirect_name} = 'boondoggle'; print $h{name}, "\n"; # prints 'boondoggle' $h{name} = 'Fred'; print $h{name}, "\n"; # still prints 'Fred' print $h{redirect_name}, "\n"; # still prints 'boondoggle'
Whether the tied hash is an improvement over simply using the code you showed depends on how deeply encapsulated you want the behavior to be. With your code it's out in the open; with the tied hash it just looks like a hash that magically does what you want. If the hash will change infrequently, you're probably better off using the code you showed, because you don't need to do it very often. But if the hash changes a lot and needs to be constantly kept up to date, the tied hash solution might be better because it does all the right work magically, behind the scenes.

With the implementation I showed above, you can't use delete. It's not hard to come up with an implementation that fixes that, but I'll leave it as an exercise because I've noticed that people tend not to upvote posts that have too much code in them.


In reply to Re: Regexs on Hash Keys by Dominus
in thread Regexs on Hash Keys by Maclir

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.