in reply to Re: stat on file name containing single quotes
in thread stat on file name containing single quotes

The line is in fact read from a text file. Here is some simplified code that reads from a file containing only the one line I am talking about here.

use 5.12.0; use strict; use warnings; use Carp; open IN,"<DirList.txt"; my $fn=<IN>; chomp $fn; say $fn; my @st=stat $fn; if (@st) { say "Array defined"; } else { say "Array undefined"; }

The result is:

E:\Music\Ray LaMontagne\2010 - God Willin' & The Creek Don't Rise Array undefined

Replies are listed 'Best First'.
Re^3: stat on file name containing single quotes
by perlpipe (Acolyte) on Jul 31, 2015 at 15:06 UTC
    Note that the "chomp" and the "say" don't seem to have a problem with the variable $fn containing a string that has single quotes and backslashes in it. So I was expecting "stat" to not have a problem. Obviously I was wrong.

      I had no problem using stat on a file path containing backslashes and apostrophes. (Just to be clear, that doesn't mean that those characters aren't indeed the problem in your environment.)

      One possible explanation for your problem above would be a simple "\r" at the end of the string after chomp was called. Any number of other "invisible" characters could also be to blame. At least output the length of the string as well. Adding surrounding punctuation to the output can help for some "invisible" characters. Using something like Data::Dumper (if you set the option to use double quotes instead of single quotes) or the Perl debugger (see the "x" command) can make such problems obvious.

      And, yes, even if you are using a native Windows build of Perl that by default replaces "\r\n" with "\n" when reading, it would replace "\r\r\n" (for example) with "\r\n" and chomp would replace that with just "\r", which would cause the behavior you showed.

      - tye        

Re^3: stat on file name containing single quotes
by perlpipe (Acolyte) on Jul 31, 2015 at 16:42 UTC

    Further testing indicates that we are getting side tracked by the single quote issue. That is if we change the input file so it specifies a file or directory name that contains extended ascii as in an accented e (0x82) then we get the same result with the stat function. Windows accepts these name. stat does not. The name I tested with came from my Adobe Acrobat installation in C:\Program Files (x86).

    Here is the modified program run against an updated input file.

    use 5.12.0; use strict; use warnings; use Carp; open IN,"<DirList2.txt"; my $fn=<IN>; chomp $fn; my @st=stat $fn; if (@st) { say "Array defined"; } else { say "Array undefined"; }

    The input was:

    C:\Program Files (x86)\Adobe\Acrobat 10.0\Acrobat\Sequences\FRA\Cr0xc20x82er des fichiers PDF accessibles.sequ

    The output was:

    Array undefined

      Yeah, that can happen.

      Win32 supports single-byte-character API calls ("A") or UTF-16 API calls (called "UNICODE" or "W"). When using "A" calls, you can only represent characters in your defined "code page" (which then get converted to UTF-16 which is what is used by the underlying OS code).

      So either change your code page or use the "W" APIs. You'll have to do some searching to figure out what is the current state of getting access to the "W" APIs from Perl. I've seen several paths for doing such with different trade-offs but it has been so long since I've looked at that so I won't throw out my faded memory of out-dated info.

      - tye