On 3/7/06, Gavin Bowlby <[EMAIL PROTECTED]> wrote:

> Could someone explain why the first case fails?

It's not failing. It's warning.

It's warning you that you're using an undefined value as if it were a
string. That's odd, Perl is saying, you seem to have expected $3 to
hold a string. But since the part of the pattern in the third pair of
parentheses was skipped, $3 is undef. When you try to use undef as a
string, Perl sees that you were wise enough to ask for warnings, so it
shouts out a warning to you.

In your second case, the part of the pattern inside the third pair of
parentheses matches something. That match happens to be the empty
string, but that's different than not being part of the match at all.
So $3 is the empty string, and all is quiet.

By the way, because the match memory number variables (like $3)
require a successful pattern match, it's best to use them only just
after a pattern match has succeeded. That usually puts them inside an
if block or while loop, which has a nice side effect improving
readability: The pattern stands out at the top of the code which uses
$3. The main effect, of course, is that you don't find yourself using
a leftover value in $1 from some *previous* pattern match when the
later match fails.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

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


Reply via email to