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

I have the following Perl script and it works just how I want it to. The only problem I have have is when executed each line of the output is tabbed (staggard) instead of left justified. This is the first time i have ever attempted to use the curses module. If there is another module that will work better let me know.

#!/usr/bin/perl use Curses; use Net::Ping; @host_array = qw(fee fie foo quan); initscr(); noecho(); cbreak(); nodelay(1); while(1) { clear (); refresh(); # High precision syntax (requires Time::HiRes) foreach $host (@host_array) { $p = Net::Ping->new(); $p->hires(); ($ret, $duration, $ip) = $p->ping($host, 5.5); printf("$host [ip: $ip] is down\n") unless $p->ping($host, 2); printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", + 1000 * $duration) if $ret; $p->close(); } sleep 2; }

thanks, Sean

Replies are listed 'Best First'.
Re: Curses formatting
by Fletch (Bishop) on Jan 21, 2010 at 22:24 UTC

    Your problem is that you're trying to intermix plain vanilla IO (printf) with curses. Curses expects you to do all output through it so that it can maintain a map of what's supposed to be on the screen. You'll want to use addstr rather than printing (although you certainly can use sprintf to format the text you display).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Curses formatting
by Khen1950fx (Canon) on Jan 21, 2010 at 22:54 UTC
    Ah, got it. This should work:
    #!/usr/bin/perl use Curses; use Net::Ping; @host_array = qw(fee fie foo quan); initscr(); cbreak(); noecho(); clear(); refresh(); endwin(); while(1) { # High precision syntax (requires Time::HiRes) foreach my $host (@host_array) { $p = Net::Ping->new(); $p->hires(1); ($ret, $duration, $ip) = $p->ping($host, 5.5); printf("$host [ip: $ip] is down\n") unless $p->ping($host, 2); printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", + 1000 * $duration) if $ret; $p->close(); } $p->close(); exit(0); }
    Update: Deleted addstr.