Capture-Tiny icon indicating copy to clipboard operation
Capture-Tiny copied to clipboard

Does only capture part of output

Open faxm0dem opened this issue 12 years ago • 4 comments

I came across a case in which only part of STDOUT is being captured:

#!/usr/bin/perl -w

use strict;
use RRDs qw/dump/;
use Capture::Tiny qw/capture_stdout/;

my $dump = capture_stdout { RRDs::dump $ARGV[0] };

Output using any RRD file:

<v>NaN</v><v>NaN</v><v>NaN</v></row>
                        <!-- 2013-06-06 12:56:40 CEST / 1370516200 --> <row><v>N
aN</v><v>NaN</v><v>NaN</v></row>
                </database>
        </rra>
</rrd>

The ending of the file is still being sent to STDOUT. The rest of the file (the beginning) is correctly passed to $dump.

faxm0dem avatar Jun 06 '13 14:06 faxm0dem

What is RRDs and what is dump() doing?

David

On Thu, Jun 6, 2013 at 10:00 AM, Fabien Wernli [email protected]:

I came across a case in which only part of STDOUT is being captured:

#!/usr/bin/perl -w use strict;use RRDs qw/dump/;use Capture::Tiny qw/capture_stdout/; my $dump = capture_stdout { RRDs::dump $ARGV[0] };

Output using any RRD file:

NaNNaNNaN N aNNaNNaN

The ending of the file is still being sent to STDOUT. The rest of the file (the beginning) is correctly passed to $dump.

— Reply to this email directly or view it on GitHubhttps://github.com/dagolden/capture-tiny/issues/10 .

David Golden [email protected] Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg

dagolden avatar Jun 06 '13 14:06 dagolden

RRDs provides the bindings for the C library to rrdtool (XS). It extracts the binary data from the rrd data file and exports it to an XML representation

faxm0dem avatar Jun 06 '13 19:06 faxm0dem

Sounds like rrdtool is doing something with filehandles directly instead of taking what's currently STDOUT. Not much can be done about that.

David

On Thu, Jun 6, 2013 at 3:48 PM, Fabien Wernli [email protected]:

RRDs provides the bindings for the C library to rrdtool (XS). It extracts the binary data from the rrd data file and exports it to an XML representation

— Reply to this email directly or view it on GitHubhttps://github.com/dagolden/capture-tiny/issues/10#issuecomment-19069528 .

David Golden [email protected] Take back your inbox! → http://www.bunchmail.com/ Twitter/IRC: @xdg

dagolden avatar Jun 06 '13 20:06 dagolden

I also tried IO::CaptureOutput with the exact same result. The following code works with RRDs::dump:

sub capture_stdout (&) {
  my $pid;
  my $fh;
  unless ($pid = open $fh, "-|") {
    die "Can't fork: $!" unless defined $pid;
    shift->();
    exit 0;
  }
  # the following is a noop, as it seems RRDs::dump already waits for its child
  while (waitpid(-1, &WNOHANG) > 0) { sleep 1 }
  return $fh;
}
my $fh = capture_stdout { RRDs::dump $ARGV[0] };
my $dump;
$dump .= $_ while <$fh>;

faxm0dem avatar Jun 07 '13 07:06 faxm0dem