Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I an writing a simple web server that receives web requests from an application (Scratch), which I want to process into commands and place in a queue. For the queue I'm trying to use a global array, then have a loop watching that queue to process when able. I can not get the global variables to work.
A cut down version of the code is:
package MyServer; use warnings; use strict; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); our @command_queue; sub handle_request { my ($self, $cgi) = @_; my $cmd_out; # Some processing here which populates $cmd_out push(@command_queue, $cmd_out); myDebug("Command queue has $#command_queue elements"); } my $pid = MyServer->new($PORT)->background(); my $quit=0; while($quit==0) { print "Commands in the queue = $#command_queue\n"; sleep(1); }
As each request comes in, the debugger reports the queue increasing in size, so command_queue is being maintained between calls to request_handler. However in the queue monitoring loop at the end, command_queue is constantly reported as empty, -1 in size.
How can I share data between the request_handler and another section of the program?
|
|---|