Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 937
Nate Glenn Avatar asked May 27 '11 19:05

Nate Glenn


People also ask

What is the difference between single quotes and double quotes in shell script?

There is no such difference between the single quote (') and double quote(“) in PowerShell. It is similar to a programming language like Python.

What is the difference between single quoted string and double quoted string in Perl?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string.

How do you escape a double quote in Perl?

If you put a back-slash \ in a double-quoted string, Perl will think you want to escape the next character and do its magic.

What is the difference between single quotes and double quotes in bash commands?

Single quotes won't interpolate anything, but double quotes will. For example: variables, backticks, certain \ escapes, etc. Enclosing characters in single quotes ( ' ) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.


2 Answers

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.

like image 175
mob Avatar answered Oct 14 '22 05:10

mob


This has nothing to do with the Perl interpreter. It is the shell that interprets commands and their arguments.

When you supply a double-quoted argument, for example "H:\Test!", the shell treats everything inside the quotes as the contents of the string, and what is passed to the Perl interpreter is the string without the quotes.

By contrast, when you supply 'H:\Test!', the shell takes the single quotes as a part of the string itself, and passes it this way to the Perl interpreter.

like image 33
Blagovest Buyukliev Avatar answered Oct 14 '22 04:10

Blagovest Buyukliev