matching chunks of data with a regular expression

L

Leif Wessman

Hi!

I'm using LWP::parallel to create several parallel requests. I would
like to handle the result from each reqest as it come. So, I've
registered a callback function to the user agent, like this:

my $res = $ua->register($req, \&callback);

In the callback function I would like to use a regular expression that
matches 9 digits. This is what I have:

sub callback {
my($chunk,$response,$protocol) = @_;
$chunk =~ /(\d{9})/;
...
}

What I wonder is what happens if I get a chunk that ends with "12345",
and the next chunk starts with "6789". Will it get matched? If not,
how can I change my code so it matches "123456789" even if it's in two
different chunks?

Thanks for any input.

Leif
 
A

Anno Siegel

Leif Wessman said:
Hi!

I'm using LWP::parallel to create several parallel requests. I would
like to handle the result from each reqest as it come. So, I've
registered a callback function to the user agent, like this:

my $res = $ua->register($req, \&callback);

In the callback function I would like to use a regular expression that
matches 9 digits. This is what I have:

sub callback {
my($chunk,$response,$protocol) = @_;
$chunk =~ /(\d{9})/;
...
}

What I wonder is what happens if I get a chunk that ends with "12345",
and the next chunk starts with "6789". Will it get matched?
No.

If not,
how can I change my code so it matches "123456789" even if it's in two
different chunks?

my $rest = '';
sub callback {
for ( $rest . shift ) {
print "$_\n" for /(\d{9})/g; # or whatever
( $rest) = /(\d*)$/;
}
}

Anno
 
B

Brian McCauley

Leif said:
I'm using LWP::parallel to create several parallel requests. I would
like to handle the result from each reqest as it come. So, I've
registered a callback function to the user agent, like this:

my $res = $ua->register($req, \&callback);

In the callback function I would like to use a regular expression that
matches 9 digits. This is what I have:

sub callback {
my($chunk,$response,$protocol) = @_;
$chunk =~ /(\d{9})/;
...
}

What I wonder is what happens if I get a chunk that ends with "12345",
and the next chunk starts with "6789". Will it get matched? If not,
how can I change my code so it matches "123456789" even if it's in two
different chunks?

For a general solution to this you could take a look at the working of
File::Stream.

This seems to have come up several times in the last few weeks I think
one of the people wanting this should bite the bullet and modify
File::Stream so that it can operate in a fashion where the chunks are
pushed not pulled.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,419
Latest member
ArturoBres

Latest Threads

Top