So (\.)?
differs from (\.?) in that there is no match in the first case, but there is a match in the second case? I'm still confused as to why the placement of the ? operator inside or outside the parentheses makes a difference. I thought the parentheses were only there to bind the result to a $N variable, not to change the operations of the pattern matching itself. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tom Phoenix Sent: Tuesday, March 07, 2006 6:51 PM To: Gavin Bowlby Cc: beginners@perl.org Subject: Re: question on pattern matching 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>