kwaping has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; $|++; use Carp; my $info = $ARGV[0] || 'string_of_data'; print outer($info); sub outer { use IO::Socket; my @ips = ('xxx.xxx.xxx.xx0','xxx.xxx.xxx.xx1','xxx.xxx.xxx.xx2'); my $port = xxxx; ### $SIG{INT} = \&buh_bye; ### per ikegami ### $SIG{ALRM} = \&fallback; ### per ikegami local $SIG{INT} = \&buh_bye; local $SIG{ALRM} = \&fallback; ### srand; ### woops, thanks again ikegami my $index = int(rand(scalar @ips)); alarm(5); my $sock = IO::Socket::INET->new($ips[$index].':'.$port); $index = fallback() unless ($sock); alarm(0); die "$! ($ips[$index])\n" unless ($sock); print $sock "$info$/"; sysread $sock, my ($text), 10000; close($sock); print "ok ($ips[$index])\n"; return $text; } sub fallback { my $signame = shift; print +($signame) ? "Caught SIG$signame! " : "$! "; print "($ips[$index])\nTrying fallback host "; $index -= 1; print "($ips[$index])\n"; $sock = IO::Socket::INET->new($ips[$index].':'.$port); return $index; } sub buh_bye { my $signame = shift; die "\nCaught SIG${signame}!\nIP: $ips[$index]\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Signal handlers and scoped variables, or Nested subs?
by ikegami (Patriarch) on May 12, 2006 at 22:24 UTC | |
by kwaping (Priest) on May 12, 2006 at 22:38 UTC | |
|
Re: Signal handlers and scoped variables, or Nested subs?
by ikegami (Patriarch) on May 12, 2006 at 22:22 UTC | |
by kwaping (Priest) on May 12, 2006 at 23:05 UTC | |
by ikegami (Patriarch) on May 13, 2006 at 06:58 UTC | |
by sgifford (Prior) on May 13, 2006 at 01:37 UTC | |
|
Re: Signal handlers and scoped variables, or Nested subs?
by kwaping (Priest) on May 16, 2006 at 17:18 UTC |