in reply to Net::Ping and STDOUT

Since you get the output you desire when printing to STDOUT, you have a problem with shell redirection, hence yours is not a Perl {question,problem} but a {shell,OS} one.

Or else you may simply have a problem with buffering. Are you trying to read from the file before the writing has terminated? Try adding e.g. $|++ at the top of your script.

Update: OTOH, it doesn't have anything to do with your problem, but generally Perl-style for loops are preferrable over C-style ones. Your script may be rewritten as (untested):

#!/usr/bin/perl -l use strict; use warnings; use Net::Ping; my @net=map "192.168.$_", qw/97 100 102 104 105 32 33 34/; my $p=Net::Ping->new or die "Can't create ping object: $!\n"; for my $net (@net) { for (16..250) { my $host="$net.$_"; warn "$host not responding!\n" and next if !$p->ping($host); print $host; } } __END__
Please note that I also made a few minor, "cosmetic", changes. E.g. I turned a print into a warn. This may not be what you want, but is often appropriate in similar situations.

Replies are listed 'Best First'.
Re^2: Net::Ping and STDOUT
by yam (Novice) on Aug 04, 2005 at 16:12 UTC
    I thought that may be the case, but this gives me the same problem on cygwin/bash and OpenBSD/ksh. I'll try your suggestion with the $!++. Thanks.
      Please note that it's $|++, not $!++.
        Ah, thanks for the correction. That fixed it. I'm still relatively new to this and the keyboard salad that is perl makes me crazy sometimes.

        Thanks again

Re^2: Net::Ping and STDOUT
by yam (Novice) on Aug 04, 2005 at 17:25 UTC
    Your hint about $| was right on the nose, thanks. I'll try to rewrite it with your stylistic input, thanks for that too. I thought that there had to be cleaner way to do the loop, but I'm not familiar with the idioms of perl.
      Well, my coding style isn't the bible. I would say it doesn't differ much from the average one of most perl programmers with some experience, but I'm sure some people would disagree. For example I like very much to use short-circuiting logical operators for flow control. And at times I tend to overuse $_. If it were me chances are that the main loop may have looked like this:
      for my $net (@net) { for (16..250) { local $_="$net.$_"; warn "$_ not responding!\n" and next if !$p->ping($_); print; } }
      And while it's very clear and immediate to me I understand that others' opinion may vary, and expressive variable name like $host may be preferable for maintainability resons.

      Also, if the script were only just a bunch of lines longer, I would have never used the -l switch. Of course I'm eagerly waiting for (Perl6's) say().