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)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.