in reply to problem with regex in perl script intended for 5.20,but while running on 5.10

The /r modifier for the tr/// and s/// operators has been added in version 5.14, see the perl 5.14 delta

Edit: you don't need it though, without /r $string =~ s/search/rep/; will apply its modifications directly on $string. The /r modifier is used to keep the original string intact and output the result to another one.

Replies are listed 'Best First'.
Re^2: problem with perl 5
by praveenchappa (Acolyte) on Mar 09, 2015 at 08:55 UTC

    ya got it thank you

Re^2: problem with perl 5
by praveenchappa (Acolyte) on Mar 09, 2015 at 08:52 UTC

    ok then what could be the solution in 5.10 ,how can replicate the same behaviour in 5.10,please help me out

      It sounds like you've already solved the problem, but for the benefit of others: The /r modifier causes the expression $string=~s/\Q$oldstring\E/$newstring/gsr to return the modified version of $string without changing the content of $string. However, since then you're just assigning that value back to $string, the expression if ($string = $string=~s/\Q$oldstring\E/$newstring/gsr) is equivalent to if ($string=~s/\Q$oldstring\E/$newstring/gs), since without /r, the $string=~s///; expression modifies $string directly.

      If you want to have the result in a second string you have to copy the input first.

      my $output = $input; $output = s/\Q$oldstring\E/$newstring/gs;

        Not quite, that second = should be a =~, and the shorter (more idiomatic, I'd say) way to write that is:

        (my $output = $string) =~ s/\Q$oldstring\E/$newstring/gs;