将PWD的最后一个值分配给perl模块中的变量



我想在perl模块中为变量$run赋值,并在if条件下进行签入。基本上,$run变量被分配了当前工作目录的最后一个值,如果perl脚本正在检查分配了变量$run的值,则在if条件下

perl模块代码脚本片段的一部分,其中已经看到了确切的问题。

ex.pm
-----
my $run = ${PWD##*/};
if ( $2 ne $run)
{
die "$0 should be run in a $run directory.";
}

有了这段代码,我遇到了编译问题。如果直接用最后一个路径值硬编码其工作良好的条件CCD_ 4也是一样的。

编译问题消息

syntax error at bin/ex.pm line 44, near ")
{"
syntax error at bin/ex.pm line 49, near "$sitepath "
BEGIN not safe after errors--compilation aborted at bin/TestConstants.pm line 72.
Compilation failed in require at bin/ex1.pm line 13.
BEGIN failed--compilation aborted at bin/ex1.pm line 13.
Compilation failed in require at ./bin/test.pl line 26.
BEGIN failed--compilation aborted at ./bin/test.pl line 26 (#1)
(F) Probably means you had a syntax error.  Common reasons include:
A keyword is misspelled.
A semicolon is missing.
A comma is missing.
An opening or closing parenthesis is missing.
An opening or closing brace is missing.
A closing quote is missing.

您不能在Perl中使用bash语法。

$run = ${PWD##*/};

右手边是用bash的方式表示";删除直到最后一个斜线的所有内容";,它在Perl中被写成

$run = $ENV{PWD} =~ s{.*/}{}r;

$run = substr $ENV{PWD}, 1 + rindex $ENV{PWD}, '/';

最新更新