Search code examples
windowscommand-linequotes

Perl command line: single vs. double quotes for directory arguments


I'm having trouble understanding command argument quotations for perl in Windows. Using the following program:

use strict;
use warnings;
use File::Find;
use File::Copy;

my $dir = shift;

die 'Usage: perl Folderize.pl <directory>' unless $dir;

die "$dir doesn't exist" unless -d $dir;

I get different results depending on if I use single or double quotes for a directory. If I call it with 'perl script.pl 'H:\Test!' it prints "'H:\Test!' doesn't exist". However, if I call it with 'perl script.pl "H:\Test!", it works just fine. Why is this happening?


Solution

  • On the command-line, quoting rules are the purview of the shell, not the program (perl). The rules for Unix shells are similar to the rules for Perl (double quote interpolates variables, single quotes don't) but the Windows "shell" has different rules. Some main differences are:

    • Single quote ' is not a special character

      C:>\ dir > 'foo'

    will create a file called 'foo' (the quotes will be included in the filename)

    • "" double quotes interpolate environment variables that look like %NAME%, but it won't try to interpret perl scalar variable names as environment variables:

      C:>\ perl -e "print '%PATH'"

    • The Windows shell will "close" your quote for you if you forget

      C:>\ perl -e "print qq/Hello world/

      Hello world

    This works even though I forgot to use the second double quote.