如何使用perl -e将命令行选项传递给Perl程序?

时间:2015-07-01 07:57:06

标签: bash perl

我想将以破折号(---)开头的命令行选项传递给我使用-e标志运行的Perl程序:

$ perl -E 'say @ARGV' -foo
Unrecognized switch: -foo  (-h will show valid options).

传递不以-开头的参数显然有效:

$ perl -E 'say @ARGV' foo
foo

如何正确地逃避这些,以便程序正确读取它们?

我尝试了很多变体,例如\-foo\\-foo'-foo''\-foo''\\-foo'。这些工作都不会产生不同的消息。 \\-foo实际上运行并输出\-foo

2 个答案:

答案 0 :(得分:4)

您可以使用-s,例如:

perl -se 'print "got $some\n"' -- -some=SOME

以上版画:

got SOME

来自perlrun

  

-s为命令上的开关启用基本开关解析   在程序名称之后,但在任何之前               filename参数(或参数 - 之前)。找到的任何开关都从@ARGV中移除并设置               Perl程序中的相应变量。以下程序打印" 1"如果该计划是               使用-xyz开关调用," abc"如果用-xyz = abc。

调用它
            #!/usr/bin/perl -s
            if ($xyz) { print "$xyz\n" }

        Do note that a switch like --help creates the variable "${-help}", which is not compliant with "use strict
        "refs"".  Also, when using this option on a script with warnings enabled you may get a lot of spurious
        "used only once" warnings.

对于简单的arg传递,请使用--,例如:

perl -E 'say "@ARGV"' -- -some -xxx -ddd

打印

-some -xxx -ddd

答案 1 :(得分:2)

只需在要转到程序的标志之前传递--,如下所示:

  perl -e 'print join("/", @ARGV)' -- -foo bar

打印

  -foo/bar