package My::Plugin::AnyEvent; ... sub execute_async { my ($self,$command, $complete) = @_; $complete ||= AnyEvent->condvar; # Send the command to Firefox $self->handle->push_write($command); # Read the response as soon as it becomes available $self->handle->push_read(line => sub { $complete->send($_[0]); # send the data to whoever waits }); $complete }; sub execute { my ($self,$command) = @_; # synchronously execute the command $self->execute_async($command)->recv };