in reply to Re: Net::SOCKS how define timeout?
in thread Net::SOCKS how define timeout?

Thanks for the reply ahmad! I was looking at alarm() and it appear to be a solution to my problem, however it breaks my code. I used like that...
eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB \n required alarm 5; my $sock = new Net::SOCKS(socks_addr => $socksserver, socks_port => $s +ocksport, protocol_version => 5); alarm 0; }; die if $@ && $@ ne "alarm\n"; # propagate errors if ($@) { print("\nTIME-OUT\n"); } else { my $con = $sock->connect(peer_addr => '10.1.1.3', peer_port => 23); #other parts of my code. }
Without this alarm() the application runs without errors, but with this new alarm() code I'm getting errors: Global symbol "$sock" requires explicit package name at ./test.pl line 48. Execution of ./test.pl aborted due to compilation errors. I have the main code, inside this main code I call 3 threads and inside this threads I call a function to test the connectivity, this function is where this example of code is located. I don't want to die(), well, I can call die(), but I can't exit the application, in the case of a timeout I just want to call "return" or something like that to return from this function to the previous function (the one used when I called the tread). Can you please give a help how to fix this example of code with alarm()? Thank you

Replies are listed 'Best First'.
Re^3: Net::SOCKS how define timeout?
by Anonymous Monk on May 08, 2010 at 06:48 UTC
Re^3: Net::SOCKS how define timeout?
by samarzone (Pilgrim) on May 08, 2010 at 08:20 UTC

    Declare the socket variable (my $sock) outside of eval so that it may be accessible outside of eval

      very strange, the code always die and with the unwanted message "Alarm clock", even if I replace in the code things like $SIG{ALRM} = sub { die "Timeout\n" }; or $SIG{ALRM} = sub { return; }; - There is a way to solve it? Thank you
        Strange indeed. Does it die on the line containing
        die if $@ && $@ ne "alarm\n";
        ? Try to change it to
        die $@ if $@ && $@ ne "alarm\n";
      Thanks guys, it solved the problem. I'm shamed, very idiot problem. The only problem now is that when the code timeout it print a crazy error "Alarm clock" that I never asked to print. I mean, I have a own message like print ("Timeout\n"); but when the timeout happens it's never called and instead it prints this "Alarm clock" that is not in my code. There is a way to replace it with my own print timeout error? Thank you
        Just override the alarm signal handler:
        $SIG{ALRM} = sub { die "Timeout\n" };
      The problem continue to die even with this change. Very strange. There is no way to completely ignore the die() and just return or do nothing? I mean, just return from the function? Well, the function that has this code is called simultaneously by different threads. May it be a problem? Maybe this alarm() is no thread safe? Any other solution? Also, why I can't change this message of "Alarm clock"? Thank you.
      I also noticed that when this unwanted "Alarm clock" appear it kill my code, what is not what I want. I just want to return from the function, not kill/exit the whole code. How to fix it? thank you