This is untested code, and given the lack of detail in your question, assumes a lot of things.

The basic idea is to start a thread that monitors a shared time value and if we ever reach that time, issues the keep alive command and resets the timer.

The class wraps the Telnet object (by composition) and overrides the cmd() method to ensure the timer gets reset before the cmd is issued. It also adds a DESTROY() method to clean up the thread when the session ends.

Adding accessors and error handling is left as an exercise.

package My::Telnet; use threads::shared; use Net::Telnet; ## Thread to keep session alive sub _keepAlive { my( $tn, $done, $keepAlive ) = @_; ## Die when signalled ## Wake up once per second while( not $done and sleep 1 ) { ## If it's time, send the keep alive cmd. if( time() >= $keepAlive ) { my @output = $tn->cmd( ## Keep alive command goes here ); ## and reset the timeout $keepAlive = time() + 30; } } } sub new { my( $class, $host, $port ) = @_; ## Create the session my $tn = Net::Telnet->new( host => $host, port => port, errmode => sub{ warn "Error: $_[ 0 ]"; }, ) or die $!; ## Some shared vars for communication my $done :shared = 0; my $keepAlive :shared = time() + 30; ## Start the thread. my $thread = threads->create( \&_keepAlive, $tn, $done, $keepAlive } or die $!; ## build the object and return it bless { done => \$done, telnet => $tn, host => $host, port => $port, thread => $thread, keepAlive => \$keepAlive; }, $class; } ## resets the timer to the time supplied ## or now + 30 seconds sub resetKeepAlive { my( $self, $timeout ) = @_; $$self->{ keepAlive } } = $timeout || ( time() + 30 ); } ## Issues a command ## Resetting the timer to prevent the next keep alive. sub cmd { my( $self, %cmdNargs ) = @_; ## reset the timer *before* issuing the command $self->resetKeepAlive; ## Do the command and return the output return $self->{ telnet }->cmd( %cmdNargs ) } ## Clean up by telling the thread to die ## waiting for it to do so ## and closing the connection. sub DESTROY { my( $self ) = @_; $$self->{ done } = 1; $self->{ thread }->join; close $self->{ telnet }; return; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Structure (timer) calling a method regularly in case of inactivity (threads are perfect) by BrowserUk
in thread Structure (timer) calling a method regularly in case of inactivity by Iceman1884

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.