简体   繁体   English

Perl 命令行:目录 arguments 的单引号和双引号

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

I'm having trouble understanding command argument quotations for perl in Windows.我无法理解 Windows 中 perl 的命令参数引用。 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!'如果我用'perl script.pl 'H:\Test!'调用它it prints "'H:\Test.'它打印“'H:\Test.' doesn't exist", However, if I call it with 'perl script.pl "H:\Test!"不存在”,但是,如果我用'perl script.pl "H:\Test!"调用它, it works just fine. ,它工作得很好。 Why is this happening?为什么会这样?

On the command-line, quoting rules are the purview of the shell, not the program (perl).在命令行上,引用规则是 shell 的权限,而不是程序 (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. Unix shell 的规则类似于 Perl 的规则(双引号内插变量,单引号不),但 Windows “shell”有不同的规则。 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)将创建一个名为'foo'的文件(引号将包含在文件名中)

  • "" double quotes interpolate environment variables that look like %NAME% , but it won't try to interpret perl scalar variable names as environment variables: ""双引号插入看起来像%NAME%的环境变量,但它不会尝试将 perl 标量变量名称解释为环境变量:

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

  • The Windows shell will "close" your quote for you if you forget如果您忘记了,Windows shell 将为您“关闭”您的报价

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

    Hello world

This works even though I forgot to use the second double quote.即使我忘记使用第二个双引号,这仍然有效。

This has nothing to do with the Perl interpreter.这与 Perl 解释器无关。 It is the shell that interprets commands and their arguments.解释命令及其 arguments 的是 shell。

When you supply a double-quoted argument, for example "H:\Test!"当您提供双引号参数时,例如"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. , shell 将引号内的所有内容视为字符串的内容,传递给 Perl 解释器的是不带引号的字符串。

By contrast, when you supply 'H:\Test!'相比之下,当您提供'H:\Test!' , the shell takes the single quotes as a part of the string itself, and passes it this way to the Perl interpreter. , shell 将单引号作为字符串本身的一部分,并以这种方式将其传递给 Perl 解释器。

Because of the drive letter I assume you work under (the thumb of) Windows.由于驱动器号,我假设您在 Windows 下工作。 Then you'll have just to accept, that double quotes are the one and only quotes.然后你只需要接受,双引号是唯一的引号。

Please consider using Getopt::Long instead of doing your own argument parsing请考虑使用 Getopt::Long 而不是自己进行参数解析

http://perldoc.perl.org/Getopt/Long.html http://perldoc.perl.org/Getopt/Long.html

    use Getopt::Long;


    GetOptions
    (
        'dir=s' => \$dir,
    ) or die ("Couldnot process arguments");

in this case dir will have directory whether the user does在这种情况下 dir 将有目录,无论用户是否

    yourscript.pl -dir = foo
    yourscript.pl -dir  "foo"
    yourscript.pl -dir  foo

If you do this如果你这样做

    GetOptions
    (
        'dir=@s' => \@dir,
    ) or die ("Couldnot process arguments");

You will get all the directories in a plain old array您将在一个普通的旧数组中获得所有目录

yourscript.pl -dir  foo -dir blah

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM