#!/usr/bin/perl
use warnings;
use strict;

use POE;
use POE::Component::Server::TCP;
use POE::Wheel::Run;

POE::Component::Server::TCP->new(
    Port  => 22224,
    ClientConnected => sub {
        $_[KERNEL]->yield("vitejte");
    },
    ClientInput => sub {
        my $data    = $_[HEAP];
        my $vypocet = $_[ARG0];

        if ($data->{cmd} or $vypocet =~ m/\\'/) {
            return;
        }

        $data->{cmd} = POE::Wheel::Run->new(
            Program      => "echo '$vypocet' | bc -l",
            StdoutEvent  => "odpoved",
            CloseEvent   => "konec",
        );
    },
    InlineStates => {
        odpoved => sub {
            $_[HEAP]->{client}->put($_[ARG0]);
        },
        konec => sub {
            delete $_[HEAP]->{cmd};
        },
        vitejte => sub {
            $_[HEAP]->{client}->put("Vitejte na POE serveru");
        },
    },
);

POE::Kernel->run();
