in reply to Re^4: Encode throws "Wide character in subroutine entry" when using XML::Simple
in thread Encode throws "Wide character in subroutine entry" when using XML::Simple

Sorry, I only sent a clip of a rather large program.

I didn't remove the preferred parser setting.

Here's a sample xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <ettx ver="2"> <table id="{4fa6cd7a-f7b6-416d-8f59-3acc0eab9bdb}" name="TestFile"> <level type="V"> <map sync="Title" src="some unicode chars"/> </level> </table> </ettx>

Here's the code where the XML parsing takes place:

package ETTX; use strict; use warnings; use XML::Simple; local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; sub new(){#scalar file name my $class = shift; my $self = { ettxFile => '', ettx => {}, }; bless $self, $class; load($self,shift) if @_ ==1; return $self; }; sub load(){#scalar file name my ($self, $ettxFile) = @_; print "loading $ettxFile"; open(my $fh, '<', $ettxFile); binmode($fh); my $xml = XML::Simple->new(); $self->{ettx} = $xml->XMLin($fh, ForceArray => ['map'], KeyAttr => {}, ) ->{table}; $self->{ettxFile} = $ettxFile; 1; }

I call it from somewhere else like this:

my $ettx = ETTX->new(); $ettx->load($ettxFile);

utf8 with BOM kills it; without is fine.

Replies are listed 'Best First'.
Re^6: Encode throws "Wide character in subroutine entry" when using XML::Simple
by ikegami (Patriarch) on Dec 14, 2010 at 00:31 UTC

    local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; is misplaced. You call load after the local falls out of scope, and thus after the local undid the assignment.

    The point of specifying PREFERRED_PARSER is to choose the fastest parser available for XML::Simple and to avoid buggy XML::SAX::PurePerl. Since you get the the encoding error when you don't specify PREFERRED_PARSER, you must be defaulting to XML::SAX::PurePerl. I take it out of my XML::SAX configuration file so it never gets selected.

    You are also using incorrect prototypes. You say your functions take no arguments, but that's obviously not true.

    Fixed:

    package ETTX; use strict; use warnings; use XML::Simple; sub new { #scalar file name my $class = shift; my $self = { ettxFile => '', ettx => {}, }; bless $self, $class; $self->load(@_) if @_; return $self; }; sub load { #scalar file name my ($self, $ettxFile) = @_; print "loading $ettxFile"; open(my $fh, '<', $ettxFile); binmode($fh); local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; my $xml = XML::Simple->new(); $self->{ettx} = $xml->XMLin($fh, ForceArray => ['map'], KeyAttr => {}, ) ->{table}; $self->{ettxFile} = $ettxFile; 1; }