kingjamesid has asked for the wisdom of the Perl Monks concerning the following question:

#!pel my $string = "C:\cFiles"; my $list = `dir /b $string`; print $list
======================= When I use "C:\cFiles"; the result is "File not found" But when I use 'C:\cFiles' I get the results What is going on with double quotes that is not giving the right results? Is \c in C:\cfiles messing it up? No idea. CLues?

Replies are listed 'Best First'.
Re: Text Processing
by Corion (Patriarch) on Jul 25, 2009 at 18:32 UTC

    Double quotes interpolate backslashes, while single quotes don't, see perlop on Quote and Quote-like Operators. Also, you'll still be better off to correctly double the backslash, into C:\\cFiles, or, even better, avoid `dir /b $string` entirely and use the following:

    use strict; use File::Glob qw(bsd_glob); my $string = "C:\\cFiles"; my $string = "C:/cFiles"; # works just as well here my $list = join "\n", bsd_glob $string;
      use File::Find::Rule 0.30; my $string = "C:/cFiles"; # works just as well here my $list = join "\n", File::Find::Rule->maxdepth(1)->in($string);
Re: Text Processing
by cdarke (Prior) on Jul 26, 2009 at 10:14 UTC
    The monks who responded above with 'C:/cFiles' were working on the fact that the MS Windows operating system itself will accept either / or \ as a directory separator. Unfortunately the same cannot be said of all applications, particularly those that indicate command-line options by the / prefix, and dir is one of them.

    Using / as a directory separator with dir gives: Invalid switch. If you are going to use badly behaved programs like the Micrsoft ones (!) then best to use \, but remember to add an extra one: 'C:\\cFiles'.
Re: Text Processing
by imrags (Monk) on Jul 27, 2009 at 06:11 UTC
    Double quotes processes the string. "\" is generally used to escape characters, here perl interprets it as \c,
    Now, depending on which machine you are on (here it's windows), so i'd suggest escaping the "\" with another one.
    my $string = "C:\\cFiles"; print $string; my $string2 = "C:/cFiles"; print $string2;
    Both of them should work, as internally windows processes the directory separation using "/" rather than "\".
    Raghu