I am struggling creating a file that contains non-ascii characters.
The following script works fine, if it is called with
use Encode 'encode';
# Text is stored in utf8 within *this* file.
my $with_smiley = $ARGV;
my $filename = 'äöü' .
($with_smiley ? '☺' : '' ).
open (my $fh, '>', encode('cp1252', $filename)) or die "open: $!";
print $fh "Filename: $filename\n";
First of all, saying "UTF-8 character" is weird. UTF-8 can encode any Unicode character, so the UTF-8 character set is the Unicode character set. That means you want to create file whose name contain Unicode characters, and more specifically, Unicode characters that aren't in cp1252.
I've answered this on PerlMonks in the past. Answer copied below.
Perl treats file names as opaque strings of bytes. That means that file names need to be encoded as per your "locale"'s encoding (ANSI code page).
In Windows, code page
1252 is commonly used, and thus the encoding is usually
cp1252 doesn't support Tamil and Hindi characters [or "☺"].
Windows also provides a "Unicode" aka "Wide" interface, but Perl doesn't provide access to it using builtins**. You can use Win32API::File's
CreateFileW, though. IIRC, you need to still need to encode the file name yourself. If so, you'd use
UTF-16le as the encoding.
* — The code page is returned (as a number) by the
GetACP system call. Prepend "
cp" to get the encoding.
** — Perl's support for Windows sucks in some respects.