- 论坛徽章:
- 0
|
一下代码是功能同等于 #!/usr/bin/perl -w, 适用ksh, sh(这段代码摘自perldoc perlrun) 。 因为有些系统不能识别 #!. 请高手解释一下是如何工作的。 此断代码实际上是 以shell 代码运行的。 当作perl代码时直接就被忽略了(因为 if 0 这条语句)
eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl -wS $0 $argv:q'
if 0;
1. eval '(exit $?0)' 作用是什么?
2. 单个的 '&' 是做什么的,难道不是后台运行吗?
3. 为什么用 ${1+"$@"}, 而不直接用 "$@"?
4. ‘if 0;’ 在shell 中语法是不对的,为什么运行在这里没问题呢?
perldoc 的原文:
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. Other systems can't control that,
and need a totally devious construct that will work under any of csh, sh, or Perl, such
as the following:
eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}'
& eval 'exec /usr/bin/perl -wS $0 $argv:q'
if $running_under_some_shell;
|
|