Note that the hexdump of your input data has a Byte Order Mark (BOM) at the front of it, which Perl counts at least as some characters.
Discounting the BOM, I get the expected output with the following program:
#!/usr/bin/perl -w
use strict;
use Encode qw/encode decode/;
open (INFILE, "<:encoding(UTF-8)", "utf8.txt") || die "blah blah blah"
+;
open (OUTFILE, ">:encoding(UTF-8)", "oututf8.txt") || die "blah blah";
binmode STDOUT, ':encoding(UTF-8)';
print "Ruler : [12345678901234567890]\n";
while (my $line = <INFILE>) {
chomp ($line);
print "Input : [$line]\n";
my $linestart = substr($line,0,20);
my $outline = $linestart;
print "20 : [$outline]\n";
print "---\n";
print OUTFILE "$outline\n";
}
close (INFILE);
To remove the BOM at the start of your file, use maybe simply
$line =~ s!^\N{BYTE ORDER MARK}!!;
| [reply] [d/l] [select] |
Thank you for the suggestion - but I get the same results. I think there is some system setting on the laptop that will not read in characters - it just does bytes.
| [reply] |
Maybe now is a good time to show the exact code you are running and the exact input (again, as hexdump) you are giving it, and also to describe what method you are using to inspect the output.
For me, on Perl 5.20, on Windows 7, with the Latin-1 codepage, I get the following output from the program I posted with the input file, which shows some more "characters" on output, but that is expected because my terminal is not set to UTF-8:
Ruler : [12345678901234567890]
Input : [YearÔÖÑJEDocSrcP_USE_DATE P_DATE CurLine]
20 : [YearÔÖÑJEDocSrcP_USE_D]
On Perl 5.20, on Windows 7, with the UTF-8 codepage (via chcp 65001), I get the following output from the program I posted with the input file, which has 20 characters (not bytes) on output, as I expect:
Ruler : [12345678901234567890]
Input : [Year♥JEDocSrcP_USE_DATE P_DATE CurLine]
20 : [Year♥JEDocSrcP_USE_D]
---
The script I'm running is:
#!/usr/bin/perl -w
use strict;
use Encode qw/encode decode/;
open (INFILE, "<:encoding(UTF-8)", "utf8.txt") || die "blah blah blah"
+;
open (OUTFILE, ">:encoding(UTF-8)", "oututf8.txt") || die "blah blah";
binmode STDOUT, ':encoding(UTF-8)';
print "Ruler : [12345678901234567890]\n";
while (my $line = <INFILE>) {
chomp ($line);
$line =~ s!^\N{BYTE ORDER MARK}!!;
print "Input : [$line]\n";
my $linestart = substr($line,0,20);
my $outline = $linestart;
print "20 : [$outline]\n";
print "---\n";
print OUTFILE "$outline\n";
}
close (INFILE);
| [reply] [d/l] [select] |