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

I was trying out some suggestions for detecting an active dial-up connection with the simplest suggestion to use ipconfig. So I had:
#!/usr/bin/perl use warnings; while(1) { open(IPCONFIG,"ipconfig|") or die "can't read from pipe $^E"; while(<IPCONFIG>) { if (/IP Address/) { print $_; } } sleep(10); }
Which works fine on NT 4 but reports warnings under W98.
C:\Pete>perl connected.pl IP Address. . . . . . . . . : 0.0.0.0 Warning: unable to close filehandle IPCONFIG properly. IP Address. . . . . . . . . : 0.0.0.0 Warning: unable to close filehandle IPCONFIG properly. IP Address. . . . . . . . . : 195.44.21.11
Both are running ActiveState build 613. If, instead of reading from a pipe I execute the command in backticks and put the result into a scalar the warning does not occur. Can anyone explain what is happening, and if there is anything I can do to prevent the warning.

Replies are listed 'Best First'.
RE: Unable to close filehandle properly
by Odud (Pilgrim) on Jun 14, 2000 at 00:12 UTC
    jcwren suggested "I can't remember if Win98 supports pipes like NT/Unix. I wonder if it's using a temp file, and when the file is empty it's automatically closing it. Perhaps the error is from trying to close an already closed file." So I changed the code to do
    close(IPCONFIG); last;
    after I had recognised the IP Address and the warning message has gone.

    Thanks for that useful advice.
(jcwren) Re: Unable to close filehandle properly
by jcwren (Prior) on Jun 13, 2000 at 23:30 UTC
    I don't know if it will fix your problem, but I would suggest closing the file at the end of the loop (just prior to the sleep(10)). This would be good programming, in any case.

    --Chris
      Tried that, it didn't make any difference. I'll play the laziness card here.