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

  • SEARCH
  • Home
  • 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 940209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:55:03+00:00 2026-05-15T21:55:03+00:00

I have some text files which I am trying to transform with a Perl

  • 0

I have some text files which I am trying to transform with a Perl script on Windows. The text files look normal in Notepad+, but all the regexes in my script were failing to match. Then I noticed that when I open the text files in NotePad+, the status bar says “UCS-2 Little Endia” (sic). I am assuming this corresponds to the encoding UCS-2LE. So I created “readFile” and “writeFile” subs in Perl, like so:

use PerlIO::encoding;

my $enc = ':encoding(UCS-2LE)';

sub readFile {
    my ($fName) = @_;
    open my $f, "<$enc", $fName or die "can't read $fName\n";
    local $/;
    my $txt = <$f>;
    close $f;
    return $txt;
}

sub writeFile {
    my ($fName, $txt) = @_;
    open my $f, ">$enc", $fName or die "can't write $fName\n";
    print $f $txt;
    close $f;
}

my $fName = 'someFile.txt';

my $txt = readFile $fName;
# ... transform $txt using s/// ...
writeFile $fName, $txt;

Now the regexes match (although less often than I expect), but the output contains long strings of Asian-looking characters interspersed with longs strings of the correct text. Is my code wrong? Or perhaps Notepad+ is wrong about the encoding? How should I proceed?

  • 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-15T21:55:04+00:00Added an answer on May 15, 2026 at 9:55 pm

    OK, I figured it out. The problem was being caused by a disconnect between the encoding translation done by the “encoding…” parameter of the “open” call and the default CRLF translation done by Perl on Windows. What appeared to be happening was that LF was being translated to CRLF on output after the encoding had already been done, which threw off the “parity” of the 16-bit encoding for the following line. Once the next line was reached, the “parity” got put back. That would explain the “long strings of Asian-looking characters interspersed with longs strings of the correct text”… every other line was being messed up.

    To correct it, I took out the encoding parameter in my “open” call and added a “binmode” call, as follows:

    open my $f, $fName or die "can't read $fName\n";
    binmode $f, ':raw:encoding(UCS-2LE)';
    

    binmode apparently has a concept of “layered” I/O handling that is somewhat complicated.

    One thing I can’t figure out is how to get my CRLF translation back. If I leave out :raw or add :crlf, the “parity” problem returns. I’ve tried re-ordering as well and can’t get it to work.

    (I added this as a separate question: CRLF translation with Unicode in Perl)

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

Sidebar

Related Questions

I have some xml files that contain text, which are displayed on my website.
I have Netbeans 7.1.2 running and I am trying to access some text files
I have some text files with a whole bunch of info in them. Most
I have some command-line ruby scripts for things like pre-processing text files before uploading
I have some html files that I want to convert to text. I have
i'm trying to read some excel files with phpexcel, which works ok so far.
I have some corrupted Gzip log files that I'm trying to restore. The files
I have a folder full of html files, some of which have the following
So I have two files, one that contains my text, and another which I
I have a text file which has a particular line something like sometext sometext

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.