Module with session timeout



examples/mock-elapsed-time/lib/MySession.pm
package MySession;
use strict;
use warnings;

my %SESSION;
my $TIMEOUT = 60;

sub new {
    return bless {}, shift;
}

sub login {
    my ($self, $username, $pw) = @_;
    # ...
    $SESSION{$username} = time;
    return;
}

sub logged_in {
    my ($self, $username) = @_;
    if ($SESSION{$username} and time - $SESSION{$username} < $TIMEOUT) {
        return 1;
    }
    return 0;
}

1;

examples/mock-elapsed-time/bin/timer.pl
use strict;
use warnings;
use 5.010;

use MySession;

my $session = MySession->new;
$session->login('foo', 'secret');
say $session->logged_in('foo');
say $session->logged_in('bar');

my $time = 61;
say "Waiting $time seconds to see what happens....";
sleep $time;
say $session->logged_in('foo');
say $session->logged_in('bar');

perl -Ilib bin/timer.pl