I have a textfile encoded in UTF-16. Each line contains a number of columns separated by tabs. For those who care, the file is a playlist TXT export from iTunes. Column #27 contains a filename.
I am reading it using Perl 5.8.8 in Linux using code similar to:
binmode STDIN, ":encoding(UTF-16)";
while(<>)
{
chomp;
my @cols = split /\t/, $_;
my $filename = $cols[26]; # Column #27 contains the filename
print "File exists!" if (-e "$filename");
}
(Please note: I've shortened this code snippet. In my actual code I do some substitutions to convert the absolute windows filename used by iTunes into a filename valid on my Linux box)
Even though the files exist, the (-e) file test does not return true. I believe it has something to do with the string being in UTF-16 but cannot figure out what the problem is. The actual filename uses only ASCII characters. And the filename prints correctly if I print the $filename variable.
Can filenames in Perl be in UTF16? Any ideas how to get this code snippet to work?
Best Answer
The UTF-16 text is processed by the :encoding layer. By the time it gets into
$_
, there's no way to tell that it was ever UTF-16. I don't think that's your issue.My guess would be that you've either got some whitespace in your filename (that you didn't notice when you tried printing it out) or you're not in the directory you think you are.
Try
and check the filename carefully. You might also
use Cwd;
and print out the current directory.