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

Hi monks, i tried to solve one problem at that time i used '=' symbol instead of '=~' by mistack but i got the output, i don't know the meaning of it. what is the different b/w - $all=s/[|.|]+/|/g; & $all=~ s/[|.|]+/|/g;

Replies are listed 'Best First'.
Re: question on regexp:
by jwkrahn (Abbot) on Mar 03, 2008 at 20:28 UTC
    $all = s/[.|]+/|/g; is short for $all = $_ =~ s/[.|]+/|/g;

    For efficiency you may want to use $all =~ tr/.|/|/s; instead of $all =~ s/[.|]+/|/g;

Re: question on regexp:
by Fletch (Bishop) on Mar 03, 2008 at 19:50 UTC

    One's an assignment of the result of a substitution on the default argument $_ (that result being the number of substitutions made), the other is specifying the target of a substitution.

    Update: expanded on what the result would be in the first case.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: question on regexp:
by kyle (Abbot) on Mar 03, 2008 at 20:02 UTC

    I'll start with...

    $all = ~s/[.|]+/|/g;

    Have a look at perlop for details, but ~ is bitwise negation (so ~1 is 4_294_967_294). The s/// operator returns the number of replacements done, so that's what the ~ would be operating on. This is what happens:

    1. The s/// replacement is performed (on the contents of $_, see perlvar).
    2. The number of replacements that were done is bitwise negated.
    3. That's assigned to $all.

    The $all =~ s/[.|]+/|/g; syntax is one I'm guessing you're familiar with. In that case, the substitution is done on the variable $all.

    Update: Oops, I answered the wrong question.