Term :: Readline unusual behavior

I am creating a shell prompt using Term :: Readline as shown below -

my $expire_time = time + 25; # 25 sec timeout for testing and shared with thread
my $prompt   = 'SHELL> ';
my $terminal = Term::ReadLine->new('Shell Interface');
my $attribs  = $terminal->Attribs;
$terminal->ornaments(0);    # disable ornaments.

while ( defined( $_ = $terminal->readline($prompt) ) ) {
    if (/^\s*quit\s*$/xms) {
        # Cleanup the global parameters
        #...some cleanup code

        # Signal the thread to terminate, and then detach
        # it so that it will get cleaned up automatically
        # I will be using thread to control this customize shell
        $thread->kill('KILL')->detach();
        last;
        }
 }

 $thread = threads->create(
        sub {
            local $SIG{KILL} = sub { threads->exit() };
            eval {
                my $buffer_interval = int( sqrt( $expire_time - time() ) );
                my $term            = Term::ReadLine->new('Handle session of Shell');
                $term->ornaments(0);

                my $message = "Do you want to extend your session?[y/n]- ";
              INNER: while ( $expire_time > time() ) {

                    my $diff = $expire_time - time();
                    sleep 1;

                    print "diff - $diff, buffer_interval - $buffer_interval\n";

                    if ( $diff == $buffer_interval ) {

                        say "Your session will expire in $diff seconds due to inactivity.";
                        while ( defined( $_ = $term->readline($message) ) )
                        {
                            if (/^y$/i) {
                                $expire_time += $timeout;
                                say "refreshed expire_time - $expire_time";
                            }
                            next INNER;
                        }
                    }
                }
                &killproc('SHELL')
                  or die "cannot kill SHELL, aborting";
            };
        }
    );

      

This thread will handle the SHELL timeout, $ expire_time is set to some value, say 90 seconds, the square root of the timeout means 9 seconds, before the user gets a warning message that your session will expire soon .. and if the user enters' y ', the session will be extended by $ expire_time.

But when a Term :: ReadLine object is created on a stream it doesn't behave correctly, sometime when you type "y" the session expands and sometimes it doesn't.

An example is given below -

expiration time - 1430279631
SHELL> diff - 25, buffer_interval - 5
diff - 24, buffer_interval - 5
diff - 23, buffer_interval - 5
diff - 22, buffer_interval - 5
diff - 21, buffer_interval - 5
diff - 20, buffer_interval - 5
diff - 19, buffer_interval - 5
diff - 18, buffer_interval - 5
diff - 17, buffer_interval - 5
diff - 16, buffer_interval - 5
diff - 15, buffer_interval - 5
diff - 14, buffer_interval - 5
diff - 13, buffer_interval - 5
diff - 12, buffer_interval - 5
diff - 11, buffer_interval - 5
diff - 10, buffer_interval - 5
diff - 9, buffer_interval - 5
diff - 8, buffer_interval - 5
diff - 7, buffer_interval - 5
diff - 6, buffer_interval - 5
diff - 5, buffer_interval - 5
Your session will expire after 5 seconds due to inactivity.
Do you want to extend your session? [Y / n] - y
SHELL>

It doesn't print a message inside while () that says the expiration time of the update is ...

Is this a problem because I'm already in some kind of Term :: Readline request and again using the Term :: Readline prompt inside one?

Is there a flushing problem?

+3


source to share





All Articles