An4ximandre has asked for the wisdom of the Perl Monks concerning the following question:

Dear venerable Monks, I am seeking wisdom about Moose Object attribute. Well I would like to know if there exists a way to define an attribute that refers to an Object, in particular an XML::Simple object. I did something like that :

 has 'xml' =>(is=>rw, isa=>'XML::Simple').

And then I set the xml attribute as this

sub set_xml{ (my $self, my $xml)=@_; $self->xml($xml); }

But each time I call this method I obtain the following error : Attribute (xml) does not pass the type constraint because: Validation failed for 'XML::Simple' with value HASH(0x3775688). I can't get rid of this error . Thanks In advance.

Replies are listed 'Best First'.
Re: Object as Moose Object attribute
by Your Mother (Archbishop) on Nov 02, 2015 at 20:08 UTC

    Sounds like your $xml is the result of XML::Simple's XMLin($xml) and not an XML::Simple->new object.

      Oh I am such an idiot, I just get it. Then what I need, is not An Xml::Simple, but the type of XMLin ... which is a data structure. Then how can I handle this as an attribute ?

      How I think I got it , I can use isa=>'Ref' ?

        :P HashRef. You're doing fine. Keep it up!

Re: Object as Moose Object attribute
by Anonymous Monk on Nov 02, 2015 at 20:00 UTC

    Can you show some short example code that reproduces the issue? If I had to guess, I'd say you're trying to store the result of a call to XMLin, which does not return an object of type XML::Simple, but returns a pure Perl data structure, so you'll probably have to set your Moose type constraint accordingly.

      Hi, thanks for the answer, Yes I am calling set_xml with an XMLin result as follows:

      my $parser = new XML::Simple; $o->set_xml($parser->XMLin($data));

      As you guess, I am far to be a perl expert, What do you mean saying 'Set your Moose type constraint accordingly ' ? Is it possible to return a pure XML::Simple object, or this means nothing in perl ? Thanks !