简体   繁体   English

perl s/this/that/r ==> “Bareword found where operator expected”

[英]perl s/this/that/r ==> “Bareword found where operator expected”

Perl docs recommend this: Perl 文档推荐这个:

$foo = $bar =~ s/this/that/r;

However, I get this error:但是,我收到此错误:

Bareword found where operator expected near
    "s/this/that/r" (#1)

This is specific to the r modifier, without it the code works.这是特定于r修饰符的,没有它代码就可以工作。 However, I do not want to modify $bar .但是,我不想修改$bar I can, of course, replace我当然可以替换

my $foo = $bar =~ s/this/that/r;

with

my $foo = $bar;
$foo =~ s/this/that/;

Is there a better solution?有更好的解决方案吗?

As ruakh wrote, /r is new in perl 5.14.正如 ruakh 所写, /r是 perl 5.14 中的新内容。 However you can do this in previous versions of perl:但是,您可以在以前版本的 perl 中执行此操作:

(my $foo = $bar) =~ s/this/that/;

There's no better solution, no (though I usually write it on one line, since the s/// is essentially serving as part of the initialization process:没有更好的解决方案,不(虽然我通常将它写在一行上,因为s///本质上是作为初始化过程的一部分:

my $foo = $bar; $foo =~ s/this/that/;

By the way, the reason for your error-message is almost certainly that you're running a version of Perl that doesn't support the /r flag.顺便说一句,您的错误消息的原因几乎可以肯定是您运行的 Perl 版本不支持/r标志。 That flag was added quite recently, in Perl 5.14.该标志是最近在 Perl 5.14 中添加的。 You might find it easier to develop using the documentation for your own version;您可能会发现使用您自己版本的文档进行开发更容易; for example, http://perldoc.perl.org/5.12.4/perlop.html if you're on Perl 5.12.4.例如,如果您使用的是 Perl 5.12.4,则为http://perldoc.perl.org/5.12.4/perlop.html

For completeness.为了完整性。 If you are stuck with an older version of perl .如果您坚持使用旧版本的perl And really want to use the s/// command without resorting to using a temporary variable.并且真的想使用s///命令而不诉诸使用临时变量。 Here is one way:这是一种方法:

perl -E 'say map { s/_iter\d+\s*$//; $_ } $ENV{PWD}'

Basically use map to transform a copy of the string and return the final output.基本上使用 map 来转换字符串的副本并返回最终输出。 Instead of what s/// does - of returning the count of substitutions.而不是s///所做的 - 返回替换计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Perl:Bareword在操作员期望的位置找到 - Perl : Bareword found where operator expected 在操作员预期的位置找到了裸字 - bareword found where operator expected Perl搜索并替换文件错误“在期望操作员的地方找到了密码” - Perl search and replace in file error “Bareword found where operator expected” Perl语法错误:在操作员期望的位置找到了裸词 - Perl syntax error: Bareword found where operator expected Perl语法错误:在操作员期望的位置找到裸字 - Perl Syntax Error: bareword found where operator expected Bareword 在 Perl 脚本中的错误处找到操作员预期的位置 - Bareword found where operator expected at Error in Perl script 在操作员预期的语法错误所在的地方找到了裸字 - bareword found where operator expected syntax error 使用运行perl s /(“ |')//命令的Windows批处理文件/命令行,如何防止在操作员预期错误的地方发现裸字? - using a windows batch file/command line that runs a perl s/("|')// command, how to do prevent a bareword found where operator expected error? 为什么我的Perl一线式报告“在操作员期望的位置发现裸字”? - Why does my Perl one-liner report “Bareword found where operator expected”? 找到在操作员期望的-e第1行“ 9A”附近的裸词 - Bareword found where operator expected at -e line 1, near “9A”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM