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

Dear monks,
I don't understand this statement and I hope you will lighten me up a bit.

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

Questions:
1 - What does it do?
2 - substitution isn't supposed to be in scalar context?

Replies are listed 'Best First'.
Re: question of substitution
by lidden (Curate) on Feb 27, 2005 at 12:11 UTC
    Try it:
    my $foo = 'abc this cba'; my $bar = 'ijk this kji'; ($foo = $bar) =~ s/this/that/; print "foo = $foo, bar = $bar\n";
      Got it! Another question: to use /x or not to sue /x Check this code
      $program =~ s { /\* .*? \*/ } []gsx;
      this will remove any comment in a c program. What's the role of /x (I believe it is for extended regulars expressions)??
        /x modifier - 'Ignore (most) whitespace and permit comments in pattern'

        perldoc perlre


Re: question of substitution
by sh1tn (Priest) on Feb 27, 2005 at 12:32 UTC
    This statement is the same as:
    $foo = $bar; $foo =~ s/this/that/;
    But not the same as:
    $foo = $bar =~ s/this/that/; #which will return the result of a substitution #because of the lower priority of assignment operator
    To make assignment/substitution in the given order you must
    define precedence which can be done with parentheses.