Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 123 lines (98 sloc) 4.063 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#!/usr/bin/perl -w -I..

# This is a simple test of "package sessions". These are similar to
# object sessions, but they work with packages instead of objects. It
# is also a simpler test than sessions.perl.

use strict;
use lib '../lib';
use POE;

#==============================================================================
# Counter is a package composed of event handler functions. It is
# never instantiated as an object here.

package Counter;
use strict;
use POE::Session;
                                        # stupid scope trick, part 1 of 3
$Counter::name = '';

#------------------------------------------------------------------------------
# This is a normal subroutine, not an object method. It sets up the
# session's variables and sets the session in motion.

sub _start {
  my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
                                        # register a signal handler
  $kernel->sig('INT', 'sigint');
                                        # initialize the counter
  $heap->{'counter'} = 0;
                                        # stupid scope trick, part 2 of 3
  $heap->{'name'} = $Counter::name;
                                        # hello, world!
  print "Session $heap->{'name'} started.\n";
                                        # start things moving
  $kernel->post($session, 'increment');
}

#------------------------------------------------------------------------------
# This is a normal subroutine, not an object method. It cleans up
# after receiving POE's standard _stop event.

sub _stop {
  my $heap = $_[HEAP];

  print "Session $heap->{'name'} stopped after $heap->{'counter'} loops.\n";
}

#------------------------------------------------------------------------------
# This is a normal subroutine, and not an object method. It will be
# registered as a SIGINT handler so that the session can acknowledge
# the signal.

sub sigint {
  my ($heap, $from, $signal_name) = @_[HEAP, SENDER, ARG0];

  print "$heap->{'name'} caught SIG$signal_name from $from\n";
                                        # did not handle the signal
  return 0;
}

#------------------------------------------------------------------------------
# This is a normal subroutine, and not an object method. It does most
# of the counting work. It loops by posting events back to itself.
# The session exits when there is nothing left to do; this event
# handler causes that condition when it stops posting events.

sub increment {
  my ($package, $kernel, $session, $heap) = @_[OBJECT, KERNEL, SESSION, HEAP];

  $heap->{'counter'}++;

  if ($heap->{counter} % 2) {
    $kernel->state('runtime_state', $package);
  }
  else {
    $kernel->state('runtime_state');
  }

  print "Session $heap->{'name'}, iteration $heap->{'counter'}.\n";

  if ($heap->{'counter'} < 5) {
    $kernel->post($session, 'increment');
    $kernel->yield('runtime_state', $heap->{counter});
  }
  else {
    # no more events. since there is nothing left to do, the session exits.
  }
}

#------------------------------------------------------------------------------
# This state is added on every even count. It's removed on every odd
# one. Every count posts an event here.

sub runtime_state {
  my ($session, $heap, $iteration) = @_[SESSION, HEAP, ARG0];
  print( 'Session ', $heap->{name},
         ' received a runtime_state event during iteration ',
         $iteration, "\n"
       );
}

#==============================================================================
# Create ten Counter sessions, all sharing the subs in package
# Counter. In a way, POE's sessions provide a simple form of object
# instantiation.

package main;

foreach my $name (qw(one two three four five six seven eight nine ten)) {
                                        # stupid scope trick, part 3 of 3
  $Counter::name = $name;
                                        # create the session
  POE::Session->create(
    package_states => [
      Counter => [ qw(_start _stop increment sigint) ]
    ]
  );
}

$poe_kernel->run();

exit;
Something went wrong with that request. Please try again.