Perl regular expressions don't like the @ ("at") sign.

Take a look at this code:

#!/usr/bin/perl

use 5.008;

$_ = "somename@address.com";
if(/\@\w+\.com/)
{
        print "\n\nmight be an email address\n\n";
}
else
{
        print "\n\nnot an email address\n\n";
}

Shouldn't the /\@\w+\.com/ evaluate as true? I've also tried:

  • /@\w+\.com/
  • /.*\@\w+\.com/
  • /.*@\w+\.com/
  • /@/
  • /\@/

I'm just not understanding what this wouldn't work. Can anyone shed any light on this?

perl is interpolating the @ as an array variable.

Run your script with -w and you'll see a warning like this:

Possible unintended interpolation of @address in string

Hi ,

The quick fix is to enclose your email address string in a single quote and not in double quote

Try this it will work :

$_ = 'somename@address.com';

Yeah. I'm new to this, so I totally didn't understand why it wouldn't work. Thanks all.