Perl s / this / that / r ==> "Bareword found where statement expected"

Perl docs recommend this:

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

However, I get this error:

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

This applies to the modifier r , without which the code works. However, I do not want to change $bar . I can of course replace

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

from

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

Is there a better solution?

+6
source share
2 answers

As Ruach wrote, /r is new in perl 5.14. However, you can do this in previous versions of perl:

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

There is no better solution, no (although I usually write it on one line, since s/// essentially serves as part of the initialization process:

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

) By the way, the reason for your error message is almost certainly because you are using a version of Perl that does not support the /r flag. This flag was added recently, in Perl 5.14. You may find it easier to develop using the documentation for your own version; e.g. http://perldoc.perl.org/5.12.4/perlop.html if you are on Perl 5.12.4.

+2
source

Source: https://habr.com/ru/post/904190/


All Articles