Having the following:
perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu>string</bubu></some>");say Dumper $h'
is parsed as:
$VAR1 = {
'bubu' => 'string'
};
but the
perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu/></some>"); say Dumper $h'
or
perl -MXML::LibXML::Simple -MData::Dumper -E '$h=XMLin("<some><bubu></bubu></some>");say Dumper $h'
prints:
$VAR1 = {
'bubu' => {}
};
Is possible to get
$VAR1 = {
'bubu' => ""
};
To be consistent with other string values?
The real code behind the question is like:
package Something {
use Moose;
has 'bar' => (is => 'ro', isa => 'Str');
has 'baz' => (is => 'ro', isa => 'Str');
}
use 5.014;
use warnings;
use XML::LibXML::Simple;
my $xml = do {local $/, <DATA>};
my $hr = XMLin($xml);
for my $node( @{$hr->{node}} ) {
my $obj = Something->new($node);
}
__DATA__
<root>
<node>
<bar>bar1</bar>
<baz>baz1</baz>
</node>
<node>
<bar>bar2</bar>
<baz/>
</node>
</root>
which dies with
Attribute (baz) does not pass the type constraint because: Validation failed for 'Str' with value HASH(0x7f91a4a92450) at /opt/anyenv/envs/plenv/versions/5.24.0/lib/perl5/site_perl/5.24.0/darwin-2level/Moose/Object.pm line 24
Moose::Object::new('Something', 'HASH(0x7f91a3f3d430)') called at l line 28
therefore i need either
- treat the empty
baz
not as{}
but as''
- or adding some coercion to the package
Something
to coerce any empty hashrefs{}
to empty strings''
.
Any idea for the easy way?
EDIT
So, the result. The accepted answer remains because it gives the answer to the above question.
But must say, after 3 days of studying the new (very complex) module XML::Twig + learning XPath
's basics i got a solution which is more clear and nicer as the XMLIn
solution.
In the XMLIn
i needed reorganize the got hashref, because wanted only few elements and with exactly defined structure. (acceptable for an constructor). Such reorganizing (deleting unwanted members, moving deeper hashref's values to arrayrefs and such) is easy in perl, but the code isn't nice and needed cope with questions like above.
Using the XML::Twig (and 2 followup questions here) the result was much cleaner and much more readable and much shorter as the XMLIn. Really is better sacrifice some time and learn (at least the basics) of the XPath
and such...