Search code examples
perlbareword

perl s/this/that/r ==> "Bareword found where operator 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 is specific to the r modifier, without it the code works. However, I do not want to modify $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?


Solution

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

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