I am struggling creating a file that contains non-ascii characters.
The following script works fine, if it is called with 0
as parameter but dies when called with 1
.
The error message is open: Invalid argument at C:\temp\filename.pl line 15.
The script is started within cmd.exe
.
I expect it to write a file whose name is either (depending on the paramter) äöü.txt
or äöü☺.txt
. But I fail to create the filename containing a smiley.
use warnings;
use strict;
use Encode 'encode';
# Text is stored in utf8 within *this* file.
use utf8;
my $with_smiley = $ARGV[0];
my $filename = 'äöü' .
($with_smiley ? '☺' : '' ).
'.txt';
open (my $fh, '>', encode('cp1252', $filename)) or die "open: $!";
print $fh "Filename: $filename\n";
close $fh;
I am probably missing something that is obvious to others, but I can't find, so I'd appreciate any pointer towards solving this.