John W. Krahn wrote:
> 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.

Wow...powerful statement.

To be honest, I got what I needed before I really payed attention to the
above part as per Rob and Tom's replies, but after re-reading, I agree.

In the above, do I assume correctly (without time to test for myself)
that 'undef' in this case undefines any instance of $1? (or $N for that
matter)?

> 
>       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:

Honestly, I hate to say I'm a beginner, but relative to others here I
won't beg otherwise. Without having to spend time reading the ?: method
(which I never use as of yet anyway), here is how I would do it now, so
I would understand it, and so would my staff who are not programmers
whatsoever, and who may have to understand it lest I get hit by a bus. I
include comments as I would if a non programmer would have to read it:

# Get the username portion, and the domain portion that we
# must verify from the input the user types in

my ($username, $domain) = split (/\@/, $email);
{... do verification}


Now that I've started a controversy, can I ask if the following method
is correct and accepted practice if I only care about the username portion?

I use the following example often instead of split()ing, and then
breaking apart an array. Note this is a simple example, it's more handy
for me in circumstances where I may be fed an array with numerous slices:

my $username = (split (/\@/, $email))[0];

Again, I have to say that the speed of the feedback was great today :)
Rob, I appreciate your input, and Tom, I don't know if you helped
Randall write the books, but it's especially exciting to see yourself
and the author of several books I own and have read active on the list.

Steve

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


Reply via email to