Perl script help.

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.

Haven’t used Perl in a long time, but my first thought was that it migght require perenthesis around the whole if conditional.

Jesus, it’s the simple things. It should read:

$user =~ s/(Away)// if $FORM{‘message’} ne ‘’;

Sheesh. :smack:

Crap. it was supposed to bold the =~ part. That was the error.

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.)

For easier debugging, say


use CGI::Carp qw(fatalsToBrowser);

at the top of the script and let us know what the browser says when the script dies.

No, I already fixed the syntax (see above). Now, I’ve just got to get everything to actually work properly. :smiley:

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.

Yeah, I did that, thanks. I’ve nearly got the mod working, just some minor bugs to iron out. Thanks for everyone’s help.