gpt4 book ai didi

shell - : # use perl eval 'exec perl -S $0 ${1+"$ @"}' set the perl version?如何

转载 作者:行者123 更新时间:2023-12-04 02:36:14 28 4
gpt4 key购买 nike

我必须调试一个旧的 perl 脚本,显然,它正在以我不理解的方式设置 perl 的版本...

: # use perl
eval 'exec perl -S $0 ${1+"$@"}'
if 0;

perl -h 表示 -S 表示“使用 PATH 环境变量查找程序文件”。 $0 是当前程序。我读到 $@ 的意思是“来自最后一个 eval 命令的 Perl 语法错误消息。”但他们为什么要加 1 呢?这一切如何组合在一起?它在做什么?

我必须调试的部分原因是它选择了我不想要的旧版本的 perl。对于其他一切,我们使用 #!/usr/bin/env perl,我怀疑它可能在做同样的事情。我还怀疑我的解决方案可能在于修复 $PATH(或防止搞砸它的代码搞砸)。但我想通过更好地了解它现在如何选择版本来解决这个问题。

感谢您的帮助!

最佳答案

这旨在通过首先将脚本视为 shell 脚本然后执行 perl 来运行路径中第一个版本的 perl。在此上下文中,${1+"$@"} 是传递给脚本的参数(如果有)。

来自 bash 手册:

${parameter:+word}

If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.

Omitting the colon results in a test only for a parameter that is unset

perlrun 中有一个类似的例子:

This example works on many platforms that have a shell compatible with Bourne shell:

#!/usr/bin/perl
eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}'
if $running_under_some_shell;

The system ignores the first line and feeds the program to /bin/sh, which proceeds to try to execute the Perl program as a shell script. The shell executes the second line as a normal shell command, and thus starts up the Perl interpreter. On some systems $0 doesn't always contain the full pathname, so the -S tells Perl to search for the program if necessary. After Perl locates the program, it parses the lines and ignores them because the variable $running_under_some_shell is never true. If the program will be interpreted by csh, you will need to replace ${1+"$@"} with $*, even though that doesn't understand embedded spaces (and such) in the argument list. To start up sh rather than csh, some systems may have to replace the #! line with a line containing just a colon, which will be politely ignored by Perl.

使用 /usr/bin/env 是做同样事情的另一种方法,是的。

关于shell - : # use perl eval 'exec perl -S $0 ${1+"$ @"}' set the perl version?如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61690376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com