Difference between revisions of "Perl POE"

From Teknologisk videncenter
Jump to: navigation, search
m (Links)
m (A simple multitasking TCP Server)
Line 25: Line 25:
  
 
POE::Kernel->run;
 
POE::Kernel->run;
 +
= Timing the lifespan of a session =
 +
<source lang="perl">
 +
sub _start_handler {
 +
    $_[HEAP]{ts_start} = time();
 +
  }
 +
  sub _stop_handler {
 +
    my $time_elapsed = time() - $_[HEAP]{ts_start};
 +
    print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
 +
  }
 +
</source>
 
exit;  
 
exit;  
 
</source>
 
</source>
 +
 
==Links==
 
==Links==
 
*[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE.pm CPAN POE]
 
*[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE.pm CPAN POE]
 
*[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE/Component/Server/TCP.pm CPAN POE::Component::Server::TCP]]
 
*[http://search.cpan.org/~rcaputo/POE-1.003/lib/POE/Component/Server/TCP.pm CPAN POE::Component::Server::TCP]]
 
[[Category:Perl]]
 
[[Category:Perl]]

Revision as of 07:52, 20 March 2009

POE - portable multitasking and networking framework for Perl

A simple multitasking TCP Server

#!/usr/bin/perl

use warnings;
use strict;
use POE qw(Component::Server::TCP);

POE::Component::Server::TCP->new(
        Port => 12345,
        ClientConnected => sub {
            print "got a connection from $_[HEAP]{remote_ip}\n";
            $_[HEAP]{client}->put("Smile from the server.");
        },
        ClientInput => sub {
            my $client_input = $_[ARG0];
            $client_input =~ tr[a-zA-Z][A-Za-z];
            $_[HEAP]{client}->put($client_input);
        },
        ClientDisconnected => sub {
            print "client from $_[HEAP]{remote_ip} disconnected\n";
        },
);

POE::Kernel->run;
= Timing the lifespan of a session =
<source lang="perl">
sub _start_handler {
    $_[HEAP]{ts_start} = time();
  }
  sub _stop_handler {
    my $time_elapsed = time() - $_[HEAP]{ts_start};
    print "Session ", $_[SESSION]->ID, " elapsed seconds: $elapsed\n";
  }

exit; </source>

Links