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

I'm relatively new to writing Perl (though I've done my best to educate myself) and I've been tasked with reverse engineering some code. What is the purpose of the tilde (~) operator at the beginning of the following line of code? (this isn't the binding pattern match operator =~)
~s/(pattern)/replacement/g;
Much thanks for your help. -Migloth

Replies are listed 'Best First'.
Re: Operator/Regex question
by ikegami (Patriarch) on Jun 03, 2009 at 16:10 UTC

    Could it have been part of a "=~" token? Bitwise negation ("~") doesn't make much sense there, but "=~" binds the result of an expression to m//, s///, tr// and similar operators. Those operators will then match against that value, and possibly modify it. If no value is bound to the operator, they use $_.

    my ($key, $val) = ( $pair =~ /^([^=]+)=(.*)/s );

    (The parens optional since "=~" has higher precedence than "=".)

    See perlop

Re: Operator/Regex question
by JavaFan (Canon) on Jun 03, 2009 at 15:59 UTC
    Without context, it would be guessing. My guess it's a typo, going unnoticed because the return value isn't used.
Re: Operator/Regex question
by kennethk (Abbot) on Jun 03, 2009 at 15:59 UTC
    As described in perlop, ~ is the bitwise negation operator. A substitution returns the number of substitutions performed, though I can't imagine a good reason why you'd want bitwise negation of that return value.

    Update: After reading JavaFan's comment below, I think you're looking at a victim of refactoring where no bugs were introduced.

Re: Operator/Regex question
by Anonymous Monk on Jun 03, 2009 at 15:59 UTC
    perlop Unary "~" performs bitwise negation, i.e., 1's complement.
    C:\>perl -e"print ~1" 4294967294 C:\>perl -e"print ~~1" 1