# Perl script help.

**URL:** https://boards.straightdope.com/t/perl-script-help/182075
**Category:** Factual Questions
**Created:** [June 15, 2003, 8:10pm UTC](https://boards.straightdope.com/t/perl-script-help/182075 "2003-06-15T20:10:51Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 8:10pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/1 "2003-06-15T20:10:51Z")

</div>

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:

```php
$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.

---

<div class="post-metadata">

### Author: ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)
#### Post date: [June 15, 2003, 8:22pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/2 "2003-06-15T20:22:44Z")

</div>

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)// ?

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 8:30pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/3 "2003-06-15T20:30:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![wolfman](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@wolfman](https://boards.straightdope.com/u/wolfman)
#### Post date: [June 15, 2003, 8:31pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/4 "2003-06-15T20:31:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 8:34pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/5 "2003-06-15T20:34:40Z")

</div>

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

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

Sheesh. :smack:

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 8:36pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/6 "2003-06-15T20:36:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)
#### Post date: [June 15, 2003, 8:37pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/7 "2003-06-15T20:37:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)
#### Post date: [June 15, 2003, 8:42pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/8 "2003-06-15T20:42:34Z")

</div>

For easier debugging, say

```auto

use CGI::Carp qw(fatalsToBrowser);

```

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

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 8:46pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/9 "2003-06-15T20:46:52Z")

</div>

> [@](#):
>
> \*Originally posted by Terminus Est \*  
> \*\*For easier debugging, say
> 
> ```auto
> 
> 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. 😃

---

<div class="post-metadata">

### Author: ![Terminus\_Est](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/terminus_est/32/3087_2.png) [@Terminus\_Est](https://boards.straightdope.com/u/Terminus_Est)
#### Post date: [June 15, 2003, 8:59pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/10 "2003-06-15T20:59:24Z")

</div>

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:

```auto

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

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [June 15, 2003, 9:03pm UTC](https://boards.straightdope.com/t/perl-script-help/182075/11 "2003-06-15T21:03:58Z")

</div>

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