On 5/12/07, Steve Bertrand <[EMAIL PROTECTED]> wrote:

my $email = '[EMAIL PROTECTED]';
$email =~ /(.*)@(.*)/;
if ($2 !~ /domain\.com/) {
        print "var 2 is bad\n";
}
print "$1\n";

At this point, what is $1? It's the value from the last successful
pattern match. But was that the test against $email, or the one
against $2? If the latter succeeded in matching (which it should,
given this data) the pattern doesn't have a $1, so it's undef.

my $email = '[EMAIL PROTECTED]';
$email =~ /(.*)@(.*)/;
if ($2 !~ /domain\.com/) {
        print "var 2 is bad\n";
}
print "$1\n";

This time, the second match failed, so $1 is left over from the first match.

The solution is generally to copy the match variables to ordinary
variables before doing further pattern matches, and to take care to
use match variables only after the match has succeeded.

But are you really trying to do something like validate an email
address? They're more complex than you may realize. (For example,
there may be more than one '@' sign in an e-mail address.) Maybe
there's a module on CPAN that could help you with whatever you're
doing.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to