Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically log out of a menu. 1

Status
Not open for further replies.

maxcrook

Programmer
Jan 25, 2001
210
GB
Hi.

I have written a menu system in Perl.

The problem is that sometimes the users don't log out at the end of the day. I'd like to put a routine in which will exit the menu and logout after 60 minutes of inactivity.

Any ideas?

Thanks in advance.
 
You need signals and alarms. Every time you read input, you set an alarm first.
Code:
sub read_input {
  local $SIG{ALRM} = sub { die "session timed out" };
  alarm 3600;
  my $input = <STDIN>;
  alarm 0;
  return $input;
};

if <STDIN> returns first, the alarm is cancelled. If the alarm fires first, the program dies. If you want to do any clean-up, you could put it in an END block or use the more elaborate
Code:
sub read_input {
  my $input;
  eval {
    local $SIG{ALRM} = sub { die 'session timed out' };
    alarm 3600;
    $input = <STDIN>;
    alarm 0;
  }
  if ( $@ && $_ eq 'session timed out' ) {
    # clean-up code here
    exit;
  }
  return $input;
};

Yours,

f

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top