Dear Monks,

I'm asking for your help in handling accented letters in the console in Windows.

I'm writing a programme that needs to use user input, both filenames (via drag and drop) and text. Everything is fine if the filenames/text is ASCII, but if they contain accented letters like á, ő, or è then everything goes to hell.

Here's a code snippet that saves text input in a file named text.txt to see if accented letters are corrupted and tests if it can find a file with funny characters in its name or path.

This works fine for me on Ubuntu with any input and it works on Windows with ASCII input, but it fails with accented letters on Windows XP. Files with characters like í in their name are not found and I just get stuff like \x82\xA0\xFB\xA3\xA1 in the output txt, along with "does not map to Unicode" errors. If I remove the binmode STDIN line, the only difference is that I get mojibake in the output file, but still nothing works.
#!/usr/bin/perl use strict; use warnings; use utf8; binmode STDIN, ':encoding(UTF-8)'; print "Type some funny characters, they will be saved in text.txt\n"; chomp (my $text = <STDIN>); print "\n\nYou typed: $text\n"; # open (OUT, ">:encoding(UTF-8)", "text.txt") or die "Can't open file: + $!"; open (OUT, ">", "text.txt") or die "Can't open file: $!"; print OUT "$text\n"; close OUT; my ($inputfile_full, $folder, $inputfile, $inputfile_noext, $ext); do { print "\nDrag and drop the input file here and press enter.\n"; chomp ($inputfile_full = <STDIN>); # strip any leading and trailing spaces and single or double quote +s $inputfile_full =~ /^ *[\"\']?(.*)[\/\\]([^\"\']*)[\"\']? *$/; # $1 = everything up to last / or \, $2 = everything from there on + to the end except ",' and spaces at the end $folder = $1; $inputfile = $2; $inputfile =~ /(.*)\.(.*)/; $inputfile_noext = $1; $ext = $2; # strip quotes $inputfile_full =~ s/^ *[\"\']?([^\"\']*)[\"\']? *$/$1/; print "\nThe file doesn't seem to exist\nFilename: $inputfile\nPat +h: $folder\n\nTry again!\n\n" unless (-e "$folder/$inputfile"); }until (-e "$folder/$inputfile"); print "\nFile (${inputfile}) found.\nPath: $folder\nPress enter to con +tinue\n"; <STDIN>;

In reply to Character encoding in console in Windows by elef

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.