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

I have a script which automates a dump of some discs. As it goes along, it writes to standard output, and I want to log the output by redirecting to a file. Here's a fragement of the script showing the general structure:
sub perform_dump { print "\nDumping the discs...\n"; foreach my $disc (@discs) { print "...$disc...\n"; local (*HIS_IN, *HIS_OUT, *HIS_ERR); my $cmd = '/usr/sbin/ufsdump'; my @args = ('0luf', '/dev/rmt/1n', $disc); my $pid = open3 (*HIS_IN, *HIS_OUT, *HIS_ERR, $cmd, @args); print while (<HIS_ERR>); wait; &barf ('dump-fail', $disc) if $? != 0; } }
...i.e. run ufsdump in a sub-process, grab the output and channel it to standard out of the perl process. The script works, but each time it print anything, it reprints everything that's been sent to standard output since the start of the programme! Howvere, it only does this with the redirect to the file; writing to the terminal comes out as expected.

Is this a Perl bug, a module bug in IPC::Open3, a feature, or did I just screw up?

Replies are listed 'Best First'.
Re: print behaving badly
by I0 (Priest) on Dec 18, 2000 at 18:56 UTC
    $|=1;
Re: print behaving badly
by merlyn (Sage) on Dec 18, 2000 at 20:15 UTC
    Maybe I'm missing it, but I don't see where $_ is changing (or even being set!) in the loop
    print while ();
    Can you explain the logic of just this loop?
      The original poster used <PRE> instead of <CODE> around the code, and so the line print while (<HIS_IN>); became print while ();
Re: print behaving badly
by repson (Chaplain) on Dec 19, 2000 at 07:25 UTC
    I don't see why you are reading from the STDERR of the process, but then I'm not familiar with ufsdump.

    However if you just want to read from STDOUT on the process the qx// command or backticks would be sufficient.

    You might also like to have a look at this thread for some ideas on using open[23].

    Update: wrote qw// instead of qx//, fixed.