in reply to Regexs on Hash Keys

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.