Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl RegEx syntax error

Tags:

regex

perl

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";
like image 295
user953217 Avatar asked Nov 16 '11 15:11

user953217


1 Answers

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";
like image 98
Quentin Avatar answered Sep 25 '22 14:09

Quentin