Hash parsing considered ugly

Mark Morgan makk384 at gmail.com
Tue Feb 28 11:01:27 GMT 2006


On 2/26/06, James Mastros <james at mastros.biz> wrote:
>
> On Sun, 2006-02-26 at 22:00 +0200, Jonathan McKeown wrote:
> > On Sunday 26 February 2006 00:27, drkjam wrote:
> >
> > >     my %h = (
> > >         foo => 1,
> > >         foo => 2,
> >
> > [leads to %h being ( foo => 2 )]
> > > Is there any way at all to get Perl to flag this with at least some
> form
> > > of warning?
> >
> > Why? Consider:
> [useful use of this snipped].
>
> You could still warn when it's done literally, which has no real useful
> use.  On the other hand, writing code to handle such a corner case seems
> hardly worth it.
>
>         -=- James Mastros
>
>

I'd be surprised if there isn't already a Tie:: module that will do as the
OP requested.  If not, then it would be likely be only an hour or so's work
to knock up your own..

Oh, sod it, a 2 minute quick-and-dirty implementation with Tie::Filter is:

use Tie::Filter;
tie %hash, "Tie::Filter", \%wrapped,
    STOREKEY => { warn "$_ already exists" if exists $wrapped{ $_ } };

$hash{ a } = 1;   # no problem
$hash{ b } = 2;   # no problem
$hash{ a } = 33;  # a already exists at...

Packaging something similar up wouldn't take long to do.

Take care,
Mark.


More information about the london.pm mailing list