$num_of_exclamation_marks = $no_of_part title thing, but this subject is perhaps the hardest thing to understand when approaching POE. If you heard about event-based programming, and you figure "okay that's simple enough, I get it" then you probably don't! falling_off_the_chair happens, you provide a callback (AKA code reference) to sub { print "hahaha\n"; } .
In event-based programming, we set up functions (either as subroutines at the top/bottom or straight away as anonymous subroutines) and then we set up whatever listens and provide it with references to the subroutines we wrote in case of certain events.
For the webserver, we'll have to write small functions in certain cases:
sub incoming_client {
my $client = shift;
print_to_client( $client, "yo yo yo. what page will you be askingz for?" );
}
sub asked_for_page {
my ( $client, $request ) = @_;
my $page = get_page($request);
give_client_the_page( $client, $page );
}
sub accept_thankyou {
my $client = shift;
print_to_client( $client, "no, thank you!\n" );
}
Now this won't help us much if no one is going to be waiting for incoming connections, right? So we need to set up a TCP server, that accepts connections and forwards it according to the event. Obviously, the TCP server will have to understand what each event means. For example, if a client sends (using HTTP) a code that requests a file, our TCP server will need to say "oh oh, it's trying to get a file, I'll forward it to the correct subroutine". So, we'll use the fictional TCP server Sawyers::TCP::Server::That::Understands::HTTP. Yes, I like lots of namespaces. Dont::Event::Get::Me::Started.
This is the code for starting the TCP server and telling it what to do in any event:
use Sawyers::TCP::Server::That::Understands::HTTP;
my $server = Sawyers::TCP::Server::That::Understands::HTTP;
$server->set_up_events( {
incoming_connection => \&incoming_client,
incoming_request => \&asked_for_page,
closing_connection => \&accept_thankyou,
echo_hello => sub { print "hi!\n"; },
} );
$server->start;
print "yay!\n";
Now, the trick here is that any code that comes after the $server->start won't be executed. Why? Simple, because $server will be running forever. Keeps an open connection and keeps waiting for events. So, there is really very little point waiting for the "yay" line, because it won't be there. Unless we set up an event that closes $server and when it will be closed, the remaining lines of code will run.
Any questions?
HTTP servers just wait (Score:1)
2 fold answer (Score:1)
2. MY HTTPD WILL HAVE A BANNER, albeit I might have to write a new HTTP protocol...
BTW, thanks so much for all the help on #poe a while ago, this is what encouraged me to write these short articles and helped me understand POE altogether!
Re: (Score:1)
The event is probably worth still having through, because once the connection is created, the server might need to do some internal accounting related to the connection. For example, firing off a new child process if we're getting close to running out of available children to handle connections (although in this case it's async so you wouldn't do that anyways).
But you get what I mean.
Re: (Score:1)