简体   繁体   中英

Perl RegEx syntax error

the following code snippet taken from http://perldoc.perl.org/perlrequick.html#Search-and-replace gives me

Bareword found where operator expected at blub.pl line 2, near "s/dogs/cats/r"

What's the problem here? I'm using Perl 5.12.4 on Windows XP.

Code:

$x = "I like dogs.";
$y = $x =~ s/dogs/cats/r;
print "$x $y\n";

You are looking at the documentation for Perl 5.14. That example does not appear in the documentation for Perl 5.12 .

You can see that it is marked as a new feature in the perl 5.13.2 delta .

You can copy the variable and then modify it to achieve the same effect in older versions of Perl.

$x = "I like dogs.";
$y = $x;
$y =~ s/dogs/cats/;
print "$x $y\n";

Or you could use the idiomatic "one-liner":

$x = "I like dogs.";
($y = $x) =~ s/dogs/cats/;
print "$x $y\n";

I'm using the same version (on Linux) and getting the same error plus

Unquoted string "r" may conflict with future reserved word

and it works when I remove r. That tutorial is from 5.14, it may be that the r feature wasn't yet implemented in 5.12.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM