I'm attempting to make sure that a host is up before I try to get some info from it, so I am using Net::Ping to test for this. The problem is when I try to redirect STDOUT, I get nothing. I can't redirect to a file and writing to a file doesn't work either though the screen output works just fine. Here are some examples of code that works and doesn't work:
# this code works # loop.pl > loop.txt has > 0 bytes use Net::Ping; my $adr = "192.168."; my @net = qw / 97. 100. 102. 104. 105. 32. 33. 34. /; my ($i, $j, $host); for ($i=0; $i<=7; $i++) { HOST: for ($j=16; $j<=250; $j++) { $host = $adr . $net[$i] . $j; print "$host\n"; } }
This, on the other hand, doesn't work:
use Net::Ping; my $adr = "192.168."; my @net = qw / 97. 100. 102. 104. 105. 32. 33. 34. /; my ($i, $j, $host); $p = Net::Ping->new() or die "Can't create ping object: $!\n"; for ($i=0; $i<=7; $i++) { HOST: for ($j=16; $j<=250; $j++) { $host = $adr . $net[$i] . $j; if (!$p->ping($host)) { print "$host not responding!\n"; next HOST; } else { print "$host\n"; } } }
The HOST tag is for a jump that happens in the else clause. The output from the second script is like thus:
192.168.97.16
192.168.97.17
192.168.97.18
192.168.97.19
192.168.97.20
192.168.97.21
192.168.97.22
192.168.97.23
192.168.97.24
192.168.97.25
192.168.97.26 not responding!
192.168.97.27
but when I do ./ploop.pl > ploop.txt, ploop.txt is a 0 byte file and if I open and write to file, still nothing gets written to it. I don't understand what is happening. Anyone have a clue? Thanks in advance

In reply to Net::Ping and STDOUT by yam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.