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

I am having trouble creating a log file for this script, I believe this is the line that is causing the trouble, if I had to guess.

my $con = Win32::Console->new(STD_OUTPUT_HANDLE);

Could someone please point me in the right direction. thanks Steve
use Win32::Console; use Net::Ping; use warnings; use strict; my $host="10.20.0.20"; my $p = Net::Ping->new("icmp", 5, 64); $p->hires(); my $count = 0; my $pGood = 0; my $pBad = 0; open (OUTPUTFILE, ">", "C:\\OUTPUT.TXT")or die $!; $| = 1; my $con = Win32::Console->new(STD_OUTPUT_HANDLE); my $highlight = $FG_GREEN | $BG_BLACK; my $normal_color = $con->Attr; $con->Cls(); print "Host: $host\n"; print "Prot: ICMP\n"; print "Timeout: 5\n"; print "Size: 64\n"; print "\n\n"; (my $x, my $y) = $con->Cursor(); until ($count == 1000) { my ($ret, $duration, $ip) = $p->ping($host, 5.5); if ($ret == 1) { my @timeData = localtime(time); my $durationa = 1000 * $duration; $con->WriteChar("$host [ip: $ip] is alive (packet return time: +$durationa ms", $x, $y); print OUTPUTFILE "<"; print OUTPUTFILE join(' ', @timeData); print OUTPUTFILE "> "; print OUTPUTFILE "$host [ip: $ip] is alive (packet return time +:$durationa ms\n"; $pGood++; my $Goodx = $x; my $Goody = $y + 5; my $Badx = $x + 20; my $Bady = $y + 5; $con->WriteChar("Successful [ $pGood ]", $Goodx, $Goody); $con->WriteChar("Unsuccessful [ $pBad ]", $Badx, $Bady); sleep(1); } else { my @timeData = localtime(time); print OUTPUTFILE "<"; print OUTPUTFILE join(' ', @timeData); print OUTPUTFILE "> "; print OUTPUTFILE "$host [ip: $ip] Dropped.\n"; $con->WriteChar("$host [ip: $ip] Dropped.\n", $x, $y); $pBad++; my $Goodx = $x; my $Goody = $y + 5; my $Badx = $x + 35; my $Bady = $y + 5; $con->WriteChar("Successful [ $pGood ]", $Goodx, $Goody); $con->WriteChar("Unsuccessful [ $pBad ]", $Badx, $Bady); } $count++; }

Replies are listed 'Best First'.
Re: Writing to file
by moritz (Cardinal) on Mar 18, 2011 at 15:02 UTC
    I am having trouble creating a log file for this script
    What kind of troubles? Do you get bluescreens? Does your computer explode?
    I believe this is the line that is causing the trouble, if I had to guess.
    Do not guess, do not believe. Test your hypothesis.
      Well, I get no errors, the program runs just fine. It created the output file, but there is nothing inside the file when the program stops. If I take out the line then the program won't run,so I am not sure how you want me to Test my Hypothesis.

        Debugging is not a magical process that only monks can achieve... You just need to learn the common tricks, and apply some imagination.

        For example, you're writing to a file, and its not working. Print out all the return values and input values as you go. And trim it down to a script that only writes a canned string to that file in order to eliminate possibilities.

        Keep chopping off code until the problem either goes away or becomes trivial.

Re: Writing to file
by ww (Archbishop) on Mar 18, 2011 at 18:56 UTC
    You might start testing your hypothesis (see moritz reply, above) by double-checking your line 13. One obvious test (and this deal with the issue raised by SteveS832001) would be to try a different directory to write to.

    However, it just so happens that I'm not at all sure that's the problem.

    You might be well served to double check how to escape the win32 path\filename... or try using non-interpolating single-quotes.

    Works for me (update:) if allowed to run for approx 15+ iterations (precise count not determined. The shock was too great... and this oddity was discovered only when I found that at significantly higher counts, the content of OUTPUT.TXT is NOT in the order which I had expected from your code, nor from the display to the terminal.)

    update2

    Hunh??? /methinks there may be some buffering issues, as my output file is NOT ALWAYS misordered, but if not, tends to have lost some of the output seen on screen.)

    FTR, the adjustments I made to OP's script were limited to changing the quotes to singles (and later, redirecting output to another drive) and testing on localhost (127.0.0.1) where I have (intranet) Apache running.

Re: Writing to file
by danwoods (Initiate) on Mar 18, 2011 at 17:48 UTC
    What are you anticipating will be written to the file? And, I'm assuming that the "log file" you're talking about will be C:\output.txt, is that correct? Also, keep in mind that modern versions of windows will require elevated permissions to be able to write to C:\ directly... Are you running this program at an elevated level?