“#!-*-perl-*-”是什么意思?

What does "#! -*-perl-*-" mean?

在 perl 文档中可以阅读:

Parsing of the #! switches starts wherever "perl" is mentioned in the line. The sequences "-*" and "- " are specifically ignored so that you could, if you were so inclined, say

#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS [=10=] ${1+"$@"}'
    if 0; to let Perl see the -p switch.

to let Perl see the -p switch.

我对评论感到困惑。 “--perl--”这一行在注释中,shebang 已经被解析。如果我写:

!/bin/sh
#! -*-python-*-
print 1

没用。另外,那些序列总是被忽略?这是否意味着它类似于“/bin/sh -perl”?这在终端上失败了。

然后它还提到了“-p”开关。但是代码中没有p开关。

您的报价未完整复制。文字实际上说

Parsing of the #! switches starts wherever "perl" is mentioned in the line. The sequences -* and - are specifically ignored so that you could, if you were so inclined, say

#!/bin/sh --
#! -*- perl -*- -p
eval 'exec perl [=10=] -S ${1+"$@"}'
    if 0;

to let Perl see the -p switch.

这里有三件事在起作用。

  • #!/bin/sh shebang 导致脚本由 sh 运行。如果发生这种情况,eval 'exec ...' 的东西是强制 shexec perl 的 hack。
  • -*- perl -*- 说明符是 Emacs mode string。但它也巧合地触发了下一个行为。
  • 第二个 #! 与上一项中的 perl 文本一起触发了所描述的选项解析。根据解析规则,模式字符串将被忽略,该行末尾的 -p 将作为 Perl 的实际选项。

也许还应该引用文档中的前面段落作为上下文:

As of Perl 5, the #! line is always examined for switches as the line is being parsed. Thus, if you're on a machine that only allows one argument with the #! line, or worse, doesn't even recognize the #! line, you still can get consistent switch behavior regardless of how Perl was invoked, even if -x was used to find the beginning of the script.

Because many operating systems silently chop off kernel interpretation of the #! line after 32 characters, some switches may be passed in on the command line, and some may not; you could even get a - without its letter, if you're not careful. You probably want to make sure that all your switches fall either before or after that 32 character boundary. Most switches don't actually care if they're processed redundantly, but getting a - instead of a complete switch could cause Perl to try to execute standard input instead of your script. And a partial -I switch could also cause odd results.

...稍后:

If the #! line does not contain the word "perl", the program named after the #! is executed instead of the Perl interpreter. This is slightly bizarre, but it helps people on machines that don't do #!, because they can tell a program that their SHELL is /usr/bin/perl, and Perl will then dispatch the program to the correct interpreter for them.

这是文档中的一个错误。脚本中的原始行是

# -*- perl -*- -p

this pull request