由于硬件问题,我最近迁移到了新服务器。旧服务器和新服务器一样运行 Centos 7。我不确定旧服务器上的 Perl 版本,但我相信它与新服务器 (5.16.3) 相同。我所有的 Perl 脚本在新服务器上运行良好,但有一个区别。旧服务器上的 $0 变量仅返回脚本名称,但是,在新服务器上,它返回完整路径和脚本名称。
有谁知道是否有配置设置某个地方来控制这个,或者为什么 Perl 会在新服务器和旧服务器上设置不同的变量?
谢谢!
$0正是提供给perl.
$ cat a.pl
#!/usr/bin/perl
use feature qw( say );
say $0;
$ perl a.pl
a.pl
$ perl ./a.pl
./a.pl
$ perl ././././a.pl
././././a.pl
$ perl /home/ikegami/a.pl
/home/ikegami/a.pl
Run Code Online (Sandbox Code Playgroud)
如果脚本作为可执行文件运行,这通常正是提供exec给执行程序的内容。[1]
$ a.pl
./a.pl <-- "." comes from $PATH.
$ ./a.pl
./a.pl
$ ././././a.pl
././././a.pl
$ /home/ikegami/a.pl
/home/ikegami/a.pl
Run Code Online (Sandbox Code Playgroud)
如果您只需要程序名称,则可以使用File::Basename的basename.
例如,
use File::Basename qw( basename );
sub usage {
warn(@_) if @_;
my $prog = basename($0);
warn("Try `$prog --help' for help\n");
exit(1);
}
Run Code Online (Sandbox Code Playgroud)