Here are my concerns:
In Perl with some module like ftp, dbi; when one method does not work with "or die" can we leave the program.
What I want is that if it crashes then I get the error message in my log.or die "Cannot login ", $ftp->message;
Here are my concerns:
In Perl with some module like ftp, dbi; when one method does not work with "or die" can we leave the program.
What I want is that if it crashes then I get the error message in my log.or die "Cannot login ", $ftp->message;
The die makes you exit the program, there is also the exit () if you prefer but I do not see what you got against the die.
You can also say that STDERR (unless this is STDOUT) is your log file and not the screen.Code:open my $log_file, '>>', 'path/file_log' or die $_; ... ... or die print $log_file "Cannot login $ftp->message";
I use log4perl for log but the thing is that I can not do this, for example, when using
Actually I do not want my program crashes when it is unable to login; so please remove the die. The problem is that if I remove it how do I know that my program has failed to login? I can use an eval for example:Code:ftp->login("$machine",'test') or die "Cannot login ", $ftp->message;
But the worry is how do I know that it occured? Under test every return code of ftp login, its a impossible mission.Code:eval { ftp->login("$machine",'test') or die "Cannot login ", $ftp->message; if ($@) { $malog->fatal("Login error");exit 1; }
Use warn instead of die.
Code:ftp->login("$machine",'test') or warn "Cannot login ", $ftp->message;
Agreed but it does not completely solves my problem. The warn will not block my program certainly and I do know how to test it in error. I am obliged to test the return code of each error and return code are different. While the die, when there is an error, occurred in my script. What I would like done, it can detect the error as the die.
warn also gives you errors on STDERR just like die. The only difference between these 2 functions is that "warn" do not force the script to stop. If you do not want to write on STDERR, redirect messages to your log file.
Bookmarks