I'd automatically qr the "key" if it's not already in the hash.
sub FETCH {
my ($self, $key) = @_;
my $is_re = (ref($key) eq 'Regexp');
return $self->{$key} if !$is_re and exists $self->{$key};
$key = qr/$key/ if !$is_re;
if (wantarray) {
return @{$self}{ grep /$key/, keys %$self };
}
else {
/$key/ and return $self->{$_} for keys %$self;
}
}
japhy --
Perl and Regex Hacker | [reply] [d/l] |
| [reply] |
| [reply] |
My knowledge of ties is somewhat lacking, and I don't know how possible it would be, but could this be modified or adapted such that instead of just the first value who's key matched the regex, could not all values whose keys match the regex be returned in some fashion. I know this wouldn't make sense programmatically for $hash{/./} (the $ implied scalar, but you could return a list ref, but this can easily get messy), but I dunno if it's possible to use something like @hash{/./} to do this. Sure, this is just simply doing the same as @array = map { $hash{$_} if /./ } keys %hash;, but it would seem to compliment the above well.
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [d/l] [select] |
I was just having a very similar conversation with
someone in the office about those possibilities. Didn't
manage to come to any firm conclusions tho' :(
One thing we did think of - you could implement
the delete function in such a way that it deleted
every key that matched the regex (still some problems
there tho' - what if the exact key of one element is also
part of another key and would therefore match the
regex?)
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] |
| [reply] |