Mumia W. wrote:
> 
> That happens because the match variables ($1, $2, ...) are only changed
> when a regular expression matches; otherwise, they are left alone.
> 
> In the first case, "$2 !~ /domain\.com/" succeeds but does not capture
> anything, so the numbered match variables are unset.
> 
> Your situation reinforces the rule that you should always test if the
> match succeeded before you attempt to use the match variables:
> 
>     my $email = '[EMAIL PROTECTED]';
>     my @f = (undef, $email =~ /(.*)\@(.*)/);

Why did you put undef in there?  It serves no useful purpose other than making
the code harder to understand for beginners.

      my @f = $email =~ /(.*)\@(.*)/;


>     (@f > 1) && ($f[2] =~ /domain\.com/ ?
>         print "$f[1]\n" : print "var 2 is bad\n" );
> 
> The test "@f > 1" is my way of testing if the match succeeded.

The rvalue conditional operator should use the returned value:

print @f > 1 && $f[ 2 ] =~ /domain\.com/
      ? $f[ 1 ]
      : 'var 2 is bad',
      "\n";



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

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


Reply via email to