Re: Tie::IxHash coercion

[email protected] (Hans Dieter Pearcey)
Newsgroups perl.moose
Message-ID <[email protected]>
On Fri, 18 Mar 2011 10:19:31 +0000, William Bellamy Bellamy <[email protected]> wrote:
> subtype 'IxHash' 
>     => as 'HashRef' 
>     => where { 
>         ref $_ eq 'HASH' 
> };
> 
> coerce 'IxHash'
> => from 'HashRef'
> => via { 
>     tie (%$_,'Tie::IxHash') unless tied $_; return $_; 
>     
> };

Your subtype does nothing.  It needs to be more restrictive than HashRef if you
want coercions from HashRef to ever fire, and it isn't.

An example:

  subtype 'IxHash', as 'HashRef', 
    where { tied $_ and (tied $_)->isa('Tie::IxHash') };

  coerce 'IxHash', from 'HashRef',
    via { my %h = %$_; tie %h, 'Tie::IxHash'; return \%h };

(You probably don't want to tie the passed-in hash reference.)

hdp.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.