Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

by gods
on Feb 11, 2000 at 00:06 UTC ( #3333=superdoc: print w/replies, xml ) Need Help??

I'm facing the same problem, though not in a testing situation.

Following Corion's advice, I'm trying to pass the server's PID (returned by its background() method) to a handler that will be responsible for killing it.

My problem is that I can't find a way to pass the PID to the handler.

I've tried several things which all end up the same way when I access the url associated to the handler: the PID's value has not been passed and Perl "Can't kill a non-numeric process ID".

I've tried this (where the setup is maximally contrived for brevity):

A) Store PID as a global in the main package and access it from the server package.

In test.pl:
use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); our $pid = $server->background();
And in Webserver.pm:
package MyWebServer; use strict; use warnings; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $main::pid; } 1;

B) Store PID as a global in the server package and set it in the main package.

In test.pl:
use strict; use warnings; use MyWebServer; my $server = MyWebServer->new(8080); $MyWebServer::pid = $server->background();
And in Webserver.pm:
package MyWebServer; use strict; use warnings; our $pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, $pid; } 1;

C) Store the PID in a separate module used by test.pl and webserver.pm.

In test.pl:
use strict; use warnings; use Pid; use MyWebServer; my $server = MyWebServer->new(8080); Pid->set($server->background());
In Webserver.pm:
package MyWebServer; use strict; use warnings; use Pid; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { kill 9, Pid->get(); } 1;
And in Pid.pm:
package Pid; use strict; use warnings; my $pid; sub get { my $package = shift; return $pid; } sub set { my $package = shift; $pid = shift; } 1;

I've tried even more esoteric things until I started to feel like Wile E. Coyote, so I think it's time I submit this problem to you.

Any advice would be welcome, and so would any explanation as to what I fail to understand.


In reply to Re: Stopping an HTTP::Server::Simple server by textual
in thread Stopping an HTTP::Server::Simple server by jaldhar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (7)
    As of 2016-10-01 22:06 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How many different varieties (color, size, etc) of socks do you have in your sock drawer?






      Results (8 votes). Check out past polls.