I’m doing some mods to a chat script for a friend, and I’m having trouble with one line. The script keeps barfing on:
$user ~= s/(Away)// if $FORM{'message'} ne '' and $user ~= /(Away)/;
What gives here? I’ve been staring at it for hours and can’t find the problem. I’m hoping a fresh pair of eyes can tell me what’s wrong. I just know it’s something stupid and obvious.
It would help if you told us what the error message was.
That being said, the $user ~= /(Away)/ expression at the end strikes me as being redundant. $user ~= s/(Away)// won’t do anything if it doesn’t see an “(Away)” string. Also, did you mean to say s/(Away)// rather than s/(Away)// ?
It’s a CGI script. The server just says “The server encountered an internal error or misconfiguration and was unable to complete your request.” Good point about the redundant check, though–I’ll change that. And, no, you don’t have to escape parentheses inside a regular expression. I did try it that way too, just in case.
Putting parens in a regex creates a chunk, which is treated by the parser as a single unit. They’re metacharacters. If you want to match them in your regex, you’ll have to escape them with backslashes. (See p. 158 of the Camel Book, 2nd ed.)
Ah, I missed that, too. Anyway, see my note about the parens. /(Away)/ is different than /(Away)/.
You can further optimize the whole thing by saying:
$user =~ s/\(Away\)// if $FORM{message};
The single quotes are redundant around a hash key. Also $FORM{message} in Boolean context returns false anyway if the scalar it represents is a null string.