Regardless of tone,
Abigail-II is correct. You should
have based your object from
Tie::Hash instead:
(update: added DELETE)
package Tie::Hash::MultiVal;
use strict;
use warnings;
use Tie::Hash;
use base qw(Tie::StdHash);
sub TIEHASH {
my $class = shift;
return bless {@_}, $class;
}
sub STORE {
my ($hash,$k,$v) = @_;
if (exists $hash->{$k}) {
if (ref $hash->{$k}) {
push @{$hash->{$k}}, $v;
} else {
$hash->{$k} = [$hash->{$k},$v];
}
} else {
$hash->{$k} = $v;
}
}
sub DELETE {
my ($hash,$k) = @_;
if (ref $hash->{$k}) {
pop @{$hash->{$k}};
$hash->{$k} = $hash->{$k}[0] if @{$hash->{$k}} == 1;
} else {
delete $hash->{$k};
}
}
1;
Now i can use this to solve the 'problem' i had over at
URI.pm bug or am i missing something? ... notice the slice ;)
use strict;
use warnings;
use URI;
use Data::Dumper;
use Tie::Hash::MultiVal;
my $uri = URI->new('http://foo.com/bar.cgi?foo=bar&foo=baz&bar=qux&baz
+=qux');
my %hash;
tie %hash, 'MyHash';
%hash = $uri->query_form();
@hash{qw(foo bar baz qux)} = qw(one two three four);
delete $hash{foo};
print Dumper \%hash;
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)