smurray has asked for the wisdom of the Perl Monks concerning the following question:

I have a small program (see below) that connects to a network power switch and turns power on and off (NetSwitchOn();). If another connection is made prior to yours the unit will reject the connection. I have added an error handler (Errmode => sub {&errorHandler()},) but the problem is that if the second sub call trys to turn on the power and if there is an error I only get one loop and then the progress bar, not retrying the telnet connection. The first call will retry until the connection is made.
--------------------code below --------------------------- # Power switch on sub # This will connect to a network power switch and apply power to the u +ut # Author: # Date: 6/2/04 # Rev: 01 sub NetSwitchOn { print <<EOB; Turning on the UUT.... ;-) EOB $telnet = Net::Telnet->new( Host => $PS2, Port => 23, Prompt => '/[$%#>] $/', Errmode => sub {&errorHandler()}, Input_log => $pwr_log, Dump_log => $dumpPW_log, ) or "return"; print "Connected to Power Switch: ", $PS2, "\n"; print "-" x 40, "\n"; $telnet->print ('/on 1'); ok ($telnet->waitfor('/Processing - please wait/'), "Turning on th +e power switch"); ok ($telnet->waitfor('/Complete/'), "Unit is on"); $telnet->print ('/x'); $telnet->close; print "Connection closed: ;-( \n "; print "-" x 40, "\n"; } #--------------------------------------------------------------------- +--------# # This routine will handle the telnet object errors. #--------------------------------------------------------------------- +--------# sub errorHandler { print "Another system must be connected..\n"; print "Will retry in 5 seconds..\n"; sleep 5; redo; }

Replies are listed 'Best First'.
Re: net::telnet errmode issue
by tachyon (Chancellor) on Jul 27, 2004 at 02:52 UTC

    I strongly suggest you read Tutorial the section on how to ask a question. Very few monks with bother to read 100+ lines of code. You probelm is one of design, or rather the lack of it. The logic you need is trivial.....

    my $connection; while(1) { # the connect method either returns a valid connection object or u +ndef, ERR_MSG print "Trying to connect....."; ($connection, my $error) = connect(); if ( $connection ) { print "Connected OK!\n"; last; } else { print "Connect failed error: $error, retry in 5 sec.\n"; sleep 5; } } # now do stuff with your $connection

    cheers

    tachyon