# perl 5.10 sub ENDOFITERATION { undef }; # some magic value that signals the end of all values sub upto { my $start = 0; my $stop = shift; return sub { if ($start < $stop) { return $start++ } else { return ENDOFITERATION }; }; }; #### # Read incoming commands - an infinite loop/generator sub get_commands { while (<>) { yield $_; }; }; sub user_session { COMMAND: { my $command = get_commands; # read one command if ($command =~ /^login (\w+)/) { my $user = $1; my $pass = get_commands; # read next line if ($user_db{$user} ne $pass) { print "User $user rejected.\n"; redo COMMAND; } else { for my $command (get_commands) { if ($command =~ /^logout/) { return } else { yield [$user, $command] }; }; }; } else { # not logged in }; }; }; sub process_sessions { for my $line (user_session()) { my ($user,$command) = @$line; print "$user: Executing $command\n"; }; };