Thanks for the more dumb-proof explanation, although I'm afraid it's still not dumb-proof enough for me.
If you're entering the file name by dragging and dropping, Windows should be encoding them using the console's code page, which means it's already properly encoded for use by open(). Don't do any decoding, and it should work.
Drag&drop would indeed be sufficient for me, although I'm not sure if there's any difference between typing a path in the console and having it pasted in with drag & drop. Either way, I certainly don't need @ARGV filename input at all.
You say drag and drop should work with plain old open(), but this is exactly what I originally tried and it failed, hence this tread. To make things clear, here's a simplified script showing what I do. It doesn't even parse the file name for path/filename/extension, it just strips whitespace and quotes and tests if the perl script can find the file.
#!/usr/bin/perl
use strict;
use warnings;
print "Drop input file in console\n";
chomp (my $file = <STDIN>);
$file =~ s/ *["']?([^"']*)["']? *$/$1/; # strip whitespace and
+quotes
if (-e "$file") {print "\nFile found, everything is fine.\nFile:>$file
+<"} else {print "\nOoops, file not found.\nFile: >$file<"}
<STDIN>;
If I drop in a file called i.txt, it finds it, so far so good. If I drop in í.txt from the same folder, the script can't find it.
open (FILE, "<:encoding(UTF-8)", "$file"); fails on í.txt just the same as (-e "$file").
Are you saying this should work out of the box? Or should I use one of the modules? Win32::Unicode::File? Or Win32API::File?
This command in Win32::Unicode::File seems to promise to do what I want:
my $fh = Win32::Unicode::File->new($mode, $file_name); # create an instance and open the file
but it's certainly not just a plain open() like you say. I haven't installed the module so I haven't tried it.
Here's the output from your script, if I start it in the folder where í.txt is saved:
Enter file path> í.txt
$qfn="\241.txt"
open í.txt: No such file or directory
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.