Hi Scott.

Scott R. Godin wrote:
> Rob Dixon wrote:
>
> > Paul Johnson wrote:
> > > Rob Dixon said:
> > >
> > > >     $data =~ m/ <([^>]*)> /x;
> > > >     my $newdata = $1;
> > >
> > > And if the match fails?
> >
> > Well I think it's likely that you'd want to do:
> >
> >     $data =~ m/ <([^>]*)> /x or die "Malformed data";
> >
> > or at least:
> >
> >     $data =~ m/ <([^>]*)> /x or next;
> >
> > as a mismatch inmplies that something's happening that
> > you don't expect. But you're quite right, I should have
> > pointed out that $1 etc. are 'sticky' and will keep their
> > values from the last successful match. I'll go for:
> >
> >     {
> >         my $newdata;
> >         $newdata = $1 if $data =~ m/ <([^>]*)> /x;
> >         :
> >     }
>
> you want to know what's failing, right?

Wrong. John was just pointing out that $1 would remain
defined from a previous regex match in my previous post.
This would mean that there was no way of telling in
retrospect if the match had suceeded. The context of
the problem was vague anyway, and I was supposing that
the match in question would be within a 'while' block.

My solution above leaves $newdata undefined if the
match fails, and is a better generalisation as the
surrounding code has all the information it could
need (including the original $data value).

> unless ($data =~ m/ <([^>]*)> /x) {
>     warn "## Error matching regex in '$data'";
>     next;
> }
> $newdata = $1;

This is the same as one of my musings above:

    $data =~ m/ <([^>]*)> /x or next;

apart from the warning call.

Cheers,

Rob






-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to