Subject: | Surprise when calling $reader in list context |
Today I got a surprising result when I called a reader in list
context. It did not return the hash I expected but instead returned
two scalars where the second was the result I wanted. Illustrated in
the following example.
Did I not read enough of the Fine Manual or is it unintended?
This is the program:
use strict;
use warnings;
use XML::LibXML;
use XML::Compile::Schema;
my $parser = XML::LibXML->new;
my $msg = $parser->parse_string(<<'EOS');
<?xml version="1.0" encoding="utf-8"?><Foo bar="Bar1"/>
EOS
my $top = $msg->documentElement;
my $xsd = $parser->parse_string(<<'EOS');
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Foo">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded"
ref="RecentFlurbls"/>
</xs:sequence>
<xs:attribute name="bar"/>
</xs:complexType>
</xs:element>
<xs:element name="RecentFlurbls">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="Flurbl"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Flurbl">
<xs:complexType>
<xs:attribute name="name"/>
</xs:complexType>
</xs:element>
</xs:schema>
EOS
my $schema = XML::Compile::Schema->new();
$schema->addSchemas($xsd);
my $read = $schema->compile(READER => 'Foo');
my $scalar = $read->($top);
my @list = $read->($top);
require Data::Dumper;
print Data::Dumper::Dumper({scalar => $scalar, list => \@list});
and this is its output:
$VAR1 = {
'scalar' => {
'bar' => 'Bar1'
},
'list' => [
'Foo',
{
'bar' => 'Bar1'
}
]
};