在 perl 中使用 -e 和 -s 开关 [英] Using -e and -s switch in perl
问题描述
我开始学习 Perl.我试过了
I am starting to lern Perl. I tried
perl -e 'print "The value of \$x=$x\n";'
给出:
The value of $x=
鉴于:
perl -s -x=10 -e 'print "The value of \$x=$x\n";'
给予
No Perl script found in input
我想将 -s
开关与 -e
开关一起使用.. 为什么它不起作用?
I would like to use the -s
switch together with -e
switch.. Why is it not working?
推荐答案
脚本本身的参数应该在之后出现,无论它是单行还是文件.虽然并非总是必要的,但您可以使用 --
将 perl 的参数与脚本的参数分开:
Arguments to the script itself should come after the script, regardless of whether it's a one-liner or a file. While not always necessary, you can separate the arguments for perl from the arguments to your script with a --
:
$ perl -s -E'say "\$x=$x"' -- -x=42
x=42
用 --
结束 perl 的参数是必要的,因为 -x
是 perl 本身理解的标志,所以你的脚本永远不会看到它.来自 perldoc perlrun
:
Ending the arguments to perl with --
is necessary because -x
is a flag understood by perl itself, so your script would never see it. From perldoc perlrun
:
告诉 Perl 该程序嵌入在一大块不相关的文本中,例如邮件消息中.前导垃圾将被丢弃,直到第一行以#!"开头;并包含字符串perl".将应用该行上任何有意义的开关.
-x
-xdirectory
tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with "#!" and contains the string "perl". Any meaningful switches on that line will be applied.
[…]
如果指定了目录名,Perl 将在运行程序之前切换到该目录.-x 开关仅控制主要垃圾的处理.程序必须以END"结束.如果有尾随垃圾要忽略;程序可以通过DATA"处理任何或所有尾随垃圾.如果需要,可以使用文件句柄.
If a directory name is specified, Perl will switch to that directory before running the program. The -x switch controls only the disposal of leading garbage. The program must be terminated with "END" if there is trailing garbage to be ignored; the program can process any or all of the trailing garbage via the "DATA" filehandle if desired.
目录(如果指定)必须紧跟在 -x 之后且中间没有空格.
The directory, if specified, must appear immediately following the -x with no intervening whitespace.
作为指定的输入 print "The value of \$x=$x\n";
不包含shebang,未找到实际脚本,因此您遇到的错误是发出.
As the specified input print "The value of \$x=$x\n";
does not contain a shebang, the actual script wasn't found and thus the error you encountered was emitted.
不要使用 -s
开关解析,除了最简单的单行代码.改用 Getopt::Long
.
Do not use -s
switch parsing for any but the most simple one-liners. Use Getopt::Long
instead.
这篇关于在 perl 中使用 -e 和 -s 开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!