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

Hello monks!

As some of you may be aware I have bee struggling with a perl script for a few days in my attempts to parse the extremely verbose output of 3par commands.

I feel I am inching closer to my goal, but I have hit a new stumbling block. For some reason this script does not output anything at all, even tho it is loaded with test print statements.

#!/usr/bin/perl use strict; use warnings; use IO::Pipe; use IO::Handle; my @systems = ('3par-S400','3par-E200'); my $system; my $output; my $command; STDOUT->autoflush(1); print "this is test statement one!\n"; foreach $system (@systems) { $output = run_command($system); STDOUT->autoflush(1); print "this test is just before the while loop!\n"; while (<$output>) { next if (m/^$/); last if (m/^Press.*/); my (@headers) = /\d\d:\d\d:\d\d/; STDOUT->autoflush(1); print "This is a test before the loop from insde while!\n"; foreach my $header(@headers) { STDOUT->autoflush(1); print "this is a test\n"; STDOUT->autoflush(1); print "$header \n"; } } close($output); } sub run_command { my $user = 'gmon'; my $system = shift; my $protocol = 'ssh'; my $command = "statvv -ni"; my $space = " "; #The following is bad but it works for now.. my $do_command = $protocol . $space . "-l $user" . $space . $system . +$space . $command; my $cmd = new IO::Pipe; $cmd->reader($do_command); return $cmd; }


It doesn't even print any errors. Any thoughts? Thanks Monks!

Replies are listed 'Best First'.
Re: script doesn't print anything
by zentara (Cardinal) on Aug 24, 2010 at 18:29 UTC
    It doesn't even print any errors. Any thoughts?

    I'm not sure why you chose IO::Pipe to run this, but the IO::Pipe docs show

    $pipe->reader(qw(ls -l)); while(<$pipe>) { print ... }
    I would think that IPC::Open3 would be more straightforward. Like this example which shows how to grab STDERR too.
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash print WRITE "$query\n"; foreach my $h ($sel->can_read) { my $buf = ''; if ($h eq \*ERROR) { sysread(ERROR,$buf,4096); if($buf){print "ERROR-> $buf\n"} } else { sysread(READ,$buf,4096); if($buf){print "$query = $buf\n"} } } } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku flash japh