in reply to Re^2: Telnet list of IP and get information stored to a file
in thread Telnet list of IP and get information stored to a file
Use lexial filehandles, the 3 argument form of open and check for success, so rather than
Do it like this:open(OUT, ">>telnet.log");
open my $out, ">>", "telnet.log" or die $!;
Then "$ip" is a useless interpolation - just use $ip.
Your "next" in the error-sub is not needed.
You open OUT both in the main-script as well as in the error-sub which won't hurt but does not achieve anything.
Depending on what you want you could either open it once and re-use the filehandle for every error, or open it for every error. If you use lexial filehandles you can open it once in the main script and reference it in the error-sub (get rid of the open there and just do "print $out "Bad connection...".)
On a more general note you should not hard-code filenames into your script as you loose a lot of flexibility (e.g. every time you run your script you overwrite the previous error-log - maybe you want to see how that changes over time).
A better approach would be to pass in the input-filename on the commandline (using "iplist1.txt" as a default) and print the errors not to a file but to STDOUT.
In this way the user of your script can decide where the error-log goes to (he could simply redirect the script-output to a file of his choosing).
Lastly use better variable-names (e.g. $error_log rather than "OUT") - you'll be glad later.
|
|---|