azmike has asked for the wisdom of the Perl Monks concerning the following question:

I am at best a part time perl sophmore and may be barking up the wrong tree, but...
I was hoping to "add" a network interface to some existing code. I found Net::Server (specifically Net::Server::Multiplex) and thought it'd be the perfect solution. I still suspect it may be but I'm stumped at the moment.

My fundamental issue is - how do I periodically get CPU cycles away from Net::Server to run my code? I assumed that what I needed to do was define "sub loop" and insert my code there, however this is either not the right answer or there is more to do.

So here's what I have (not sure if I should repost all the code here?). I started with samplechat.pl included with Net::Server. I added the following at the end:
sub loop { my ($self, @args) = @_; # sleep 1; print STDERR "Execute my code here...?\n"; $self->SUPER::loop(@args); }
My (most likely incorrect) assumption was that the net effect of this was to execute my code then "$self->SUPER::loop(@args);" would return control to the main loop. However The print statement is only executed once. Yet the chat server continues to run.

Anyone care to throw me a bone?

-- Thanks, Mike

Replies are listed 'Best First'.
Re: How to use Net::Server
by ikegami (Patriarch) on Oct 23, 2008 at 18:58 UTC

    Like the name implies, loop loops.

    while( $self->accept ){ $self->run_client_connection; last if $self->done; }

    It doesn't return until shutdown, as implied by the "PROCESS FLOW" section.

    That's all I could find out. I can't help you solve your problem, but I thought you'd appreciate this information.

Re: How to use Net::Server
by ikegami (Patriarch) on Oct 23, 2008 at 19:24 UTC

    I think I found a solution. Completely untested.

    Change

    __PACKAGE__->run();

    to

    # Call back run_dequeue every $seconds seconds. __PACKAGE__->new( check_for_dequeue => $seconds )->run();
    and create
    sub run_dequeue { my ($self) = @_; # Can't spend long here because it blocks the entire server. ... }
      Awesome! I need to poke around a bit to understand what that is doing but at first look I think it's exactly what I'm looking for!

      Thank you!