praveenchappa has asked for the wisdom of the Perl Monks concerning the following question:

hi my snippet is working fine in perl 5.14 and above but throwing an error in perl 5.10, may be the syntax is not supproted,help me in rewriting it

if( $string=$string=~ s/\Q$oldstring\E/$newstring/gsr )//error here { .... ... ....... }

error info

Bareword found where operator expected at removestring.pl line 48, near "s/\Q$oldstring\E/$newstring/gsr"

syntax error at removestring.pl line 48, near "s/\Q$oldstring\E/$newstring/gsr"

syntax error at removestring.pl line 61, near "}"

  • Comment on problem with regex in perl script intended for 5.20,but while running on 5.10
  • Download Code

Replies are listed 'Best First'.
Re: problem with perl 5
by Eily (Monsignor) on Mar 09, 2015 at 08:47 UTC

    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.

      ya got it thank you

      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;
Re: problem with perl 5
by Tux (Canon) on Mar 09, 2015 at 14:47 UTC

    Perl != C++: please use # as comment introducer. // is the defined-or operator and very confusing in questions that deal with regular expressions.

    I'd advice to never ever use // comment style anywhere at all. ever. It is allowed in C99, and in that not even every C-compiler that claims to be C99 will accept it. It will break every C89 compiler (maybe not all gcc versions, but still). // outside of C++ should die.


    Enjoy, Have FUN! H.Merijn
Re: regex works in 5.14 but not in 5.10
by Anonymous Monk on Mar 09, 2015 at 22:37 UTC
    Your question was answered, but man that was a terrible title you chose. Just really awful.

    What you chose: "problem with perl 5" says absolutely nothing about the problem you actually have. It says "I don't like Perl 5" and you don't even realize it. Sure, the rest of us do and we correct your mistakes for you but don't you wish you could communicate more effectively in your life?

    Next time, consider explaining your actual problem -- which was that your regex works in one VERSION of Perl but not another. For example, "Regex works in Perl5.14 but not Perl5.10"

    After all, isn't your labor supposed to be cheaper than mine? Now we know ...

      It says "I don't like Perl 5"

      No, it doesn't. It's just vague. Considering that happens all the time here, this reply is unreasonably nasty, and I'm surprised it got upvoted so quickly, or even at all - I guess you're not the only one having a bad day. A minor admonition and a link to How do I compose an effective node title? would have sufficed.