Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:50:28+00:00 2026-05-24T16:50:28+00:00

I am writing a Perl script that needs to extract some data from an

  • 0

I am writing a Perl script that needs to extract some data from an XML file.

The XML file itself is encoded using UTF-8. For some reason, however, what I extract from the file ends up being encoded as ISO-8859-1. The documentation states that whatever is passed to my handlers should be UTF-8, but it just isn’t.

The parser is basically something like this:

my $parser = XML::Parser->new( Handlers => {
    # Some unrelated handlers here
    Char => sub {
        my ( $expat, $string ) = @_;
        if ( exists $data->{$curId}{$curField} ) {
            $data->{$curId}{$curField} .= $string;
        } else {
            $data->{$curId}{$curField} = $string;
        }
    } ,
} );

I have tried the following variants for actually parsing:

  • file parsed directly through $parser->parsefile, no options;
  • file parsed directly through $parser->parsefile, with the ProtocolEncoding option;
  • file opened using open( $handle , "<file.xml" ) then parsed through $parser->parse;
  • file opened using open( $handle , '<:utf8' , "file.xml" ) then parsed through $parser->parse.

In addition, I have tried each version with and without the <?xml encoding="utf-8"?> header in the file.

In all cases, what ends up in $data->{$curId}{$curField} is encoded using ISO-8859-1.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:50:29+00:00Added an answer on May 24, 2026 at 4:50 pm

    I know you already found an answer from Michel in the comments, but I’ll add a few things. With any encoding, you have to be strict about knowing what you’re taking in and what you are sending out. If you need something, don’t rely on the environment; eventually someone else will use your program and have a screwed-up environment.

    When you are reading a file, don’t use the ‘:utf8’ layer. That doesn’t care if the octets are actually UTF-8:

     open my $fh, '<:encoding(UTF-8)', $filename or ...;
    

    No matter what you think your output handle is, set it explicitly. There are a variety of ways to do this:

     use open ':encoding(utf8)';
    

    From the command-line, you can use the -C switch with the S flag to make the standard handles UTF-8:

     perl -CS input.xml
    

    Tom Christiansen has a long list of things you need to pay attention to.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Perl script that needs to transfer files between computers using
I'm writing a perl script that mimics gcc. This my script needs to process
I'm collecting some data via a Perl script. The data needs to be reviewed
I'm writing a script to extract some useful data about a series of chemical
I'm writing a script in Perl that needs to run at the same time
I am writing a perl script that should login to Gowalla, fetch some information
I am writing a perl script that manipulates password protected zip files. Consequently I
I'm writing a Perl script and would like to use a n-ary tree data
I'm writing a complex script that takes the XML backup of a Blogger blog
I am writing a perl script that will list the hotfixes installed in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.