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

I'm using Net::Telnet::Cisco, How do I keep the script from dying if the login fails. I'm testing a list of login attributes for many devices and I know some will logins will fail, but I want the script tell me and continue, not die. Something like-
$t->login(Name => $login_att[11], Password =>$login_att[12]) || warn "Can't Login: " . + $t->errmsg;

Replies are listed 'Best First'.
Re: Logging into multiple devices
by Arunbear (Prior) on Oct 26, 2004 at 14:44 UTC
    Assuming that the login method is raising the exception:
    eval { $t->login( Name => $login_att[11], Password => $login_att[12]) }; warn "Can't Login: $@" if $@;
Re: Logging into multiple devices
by gaal (Parson) on Oct 26, 2004 at 14:49 UTC
    I'm not familiar with this module, but in general you can catch calls to die by wrapping the fatal code with eval. Your code will look something like this:

    eval { $t->login(Name => $login_att[11], Password =>$login_att[12]); 1 } or warn "Can't Login: " . $t->errmsg;

    In the general case, $@ has the error you would normally get printed on stderr when the script died.

Re: Logging into multiple devices
by wilbur (Novice) on Oct 26, 2004 at 15:03 UTC
    Thank you for the replies, "eval" will come in handy. I should have known that. I just found another answer, $t->errmode("return"); also does the trick.
      What works best is up to you to decide, of course, since you know your how the rest of your code is organized.

      die and eval present a different conceptual model from having functions place error codes in return values; in my experience, they can seriously simplify code -- for instance where the code has long failure-prone flows.