develooper Front page | perl.perl6.users | Postings from April 2020

Using slurp to read in a utf16 file

Thread Next
From:
Joseph Brenner
Date:
April 26, 2020 22:20
Subject:
Using slurp to read in a utf16 file
Message ID:
CAFfgvXXb7-fYQt1nkLY8w8sSC8FRNKiRk-miUfR6diUNQaevqg@mail.gmail.com
Looking at the documentation for slurp, it looks as though there's a
convenient "enc" option you can use if you're not reading utf8 files.
So I thought this would work:

   my $contents = slurp $file, enc => "utf16";

It's not doing what I expected... Raku acts like there's nothing in $contents.

Here's the test code I've been using:

# ሀⶀ䷼ꪪⲤⲎ
my $unichar_str =
     "\x[1200]\x[2D80]\x[4DFC]\x[AAAA]\x[2CA4]\x[2C8E]";

my $file = "/home/doom/tmp/stuff_in_utf16.txt";
my $fh = $file.IO.open( :w, :enc("utf16") );
spurt $fh, $unichar_str;

# read entire file as utf16 Str
my $contents = slurp $file, enc => "utf16";
my $huh = $contents.gist;
say "contents: $contents";  #  contents:
say $contents.elems;        # 1

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About