Why doesn't on_eof get called on this example?

Konstantin A. Pustovalov konstantin.pustovalov at gmail.com
Tue Jun 24 12:50:38 CEST 2014


>>
>> I think that when there is no on_read handler and read queue is empty 
>> ( there is no outstanding push_read requests ) AE::Handle is unable 
>> to decide whether to call on_eof or on_error. You should be able to 
>> construct AE::handle with no on_read and empty queue. Then make 
>> push_read( line => ) request and get on_error called.
>
>
> Why is AE::Handle unable to decide? Why couldn't it just call on_eof 
> in that case?

man clearly says:
 > Note that, unlike requests in the read queue, an|on_read|callback 
doesn't mean you/require/some data: if there is an EOF and there are 
outstanding read requests then an error will be flagged. With 
an|on_read|callback, the|on_eof|callback will be invoked.

In other words, say, you are writing script which parses CSV file. The 
format is line-oriented. Script reading header line might be something like:

my $cv = AE::cv;
my $h = AnyEvent::Handle->new(
     fh => $fh,
     on_error => sub {
         my( $hdl, undef, $msg ) = @_;
         $hdl->destroy;
         $cv->croak( $msg );
     }
);
$h->push_read( line => sub {
     my( undef, $header ) = @_;
     eval { parser->parse( $header ) };
     if( $@ ne '' ) {
         $cv->croak( "Parse error: $@" )
     } else {
         $cv->send;
     }

});
$cv->recv;

ie, when script expects header line from the file, EOF before first \n 
is actually an error and is handled as an error in this code, on_eof 
should not be called.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.schmorp.de/pipermail/anyevent/attachments/20140624/4740fe98/attachment.html>


More information about the anyevent mailing list