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

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

Replies are listed 'Best First'.
Re^4: Net::SOCKS how define timeout?
by perlmonks12 (Novice) on May 08, 2010 at 18:31 UTC
    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";
Re^4: Net::SOCKS how define timeout?
by perlmonks12 (Novice) on May 08, 2010 at 20:27 UTC
    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.
Re^4: Net::SOCKS how define timeout?
by perlmonks12 (Novice) on May 08, 2010 at 18:03 UTC
    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" };
Re^4: Net::SOCKS how define timeout?
by perlmonks12 (Novice) on May 08, 2010 at 18:25 UTC
    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