【问题标题】:bareword found where operator expected syntax error裸字在操作员预期的语法错误处找到
【发布时间】:2019-01-14 12:49:53
【问题描述】:

我是 perl 的新手。无论如何,我试图调试自己。但我无法确定

语法错误是

Bareword found where operator expected at D:\DevelopmentLab\softwares\sc-master\sc.pl line 468, near "s%/%_%gr"
syntax error at D:\DevelopmentLab\softwares\sc-master\sc.pl line 468, near "s%/%_%gr"
Execution of D:\DevelopmentLab\softwares\sc-master\sc.pl aborted due to compilation errors.

代码是

sub getBackupFileName {
my ($containerName, $volume) = @_;
my $backupFileName = $volume =~ s%/%_%gr;
return "${containerName}_${backupFileName}.tar";
}

任何帮助将不胜感激..

【问题讨论】:

  • 您使用的是哪个版本的 Perl。运行perl -v 的结果。 s 命令的 r 选项是新的。您在示例中的代码在 Perl 5.20.2 下编译
  • 我使用的是 v5.12.3

标签: perl


【解决方案1】:

s/.../.../ 上的 /r 选项是在 Perl 5.14(2011 年)中添加的。如果您使用的是早期版本,您的代码将无法正常工作。我没有可用于测试的早期版本,因此我无法确定它是否会给出您所看到的错误消息。

让这个在早期版本的 Perl 上工作的最简单的修复方法是这样的:

sub getBackupFileName {
  my ($containerName, $volume) = @_;

  (my $backupFileName = $volume) =~ s%/%_%g;

  return "${containerName}_${backupFileName}.tar";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2019-01-23
    • 2019-10-09
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    相关资源
    最近更新 更多