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

Hi Monks I'm using 7zip inside a Perl script but for the life of me I can't get Perl to output what 7zip is doing in real time. I get output from 7zip after it has achived some stuff first. Running the same command through a command prompt in Windows each time 7zip compresses a file it tells you so. The files are large 4GB+

I've tried multiple flush methods found via your site.

#local $| = 1; #$| = 0; #$|++; #use constant {OUTPUT_AUTOFLUSH_BUFFERED =>0, # OUTPUT_AUTOFLUSH_UNBUFFERED=>1}; #$| = OUTPUT_AUTOFLUSH_UNBUFFERED; #use IO::Handle; #STDERR->autoflush(1); #STDOUT->autoflush(1);

Example below

$exclude_list = " -x!*RECYCLE.BIN -x!*Thumbs.db -x!*desktop.ini"; print "$now $package: Exclude List: " . $exclude_list . "\n\n"; $now = timestamp(); print "$now $package: Commencing ZIP to $target_path\n"; chdir $source or die "$package: Could not chdir $source, Error: $!\n"; #my $command = $zip_cmd . ' a -r -mx0 -tzip' . $exclude_list . ' ' . $ +sysvolume . ' ' . $target_zip . ' '; # Form the Zip command #my $command = "$zip_cmd a -r -mx0 -tzip $exclude_list $sysvolume $tar +get_zip "; # Form the Zip command my $result = `( $command ) 2>&1`; #$monitor_flag and print STDOUT "$command\n"; $monitor_flag and print STDOUT "$result\n"; my $ZIP = undef; open($ZIP,"$result |") or die "$package: Could not open pipe for $resu +lt, Error: $!\n"; #open($ZIP,"$command |") or die "$package: Could not open pipe for $co +mmand, Error: $!\n"; while (my $line = <$ZIP>) { print $line; $trace and print STDOUT $line; } close $ZIP;

http://www.perlmonks.org/?node_id=960000

http://www.perlmonks.org/?node_id=280025

http://www.perlmonks.org/?node=How%20do%20I%20flush%2Funbuffer%20an%20output%20filehandle%3F%20Why%20must%20I%20do%20this%3F

http://www.perlmonks.org/?node_id=20590

http://www.perlmonks.org/?node_id=669369

http://www.perlmonks.org/?node_id=20590

http://www.perlmonks.org/?node_id=280025

Replies are listed 'Best First'.
Re: Caturing 7zip output with Perl
by hippo (Archbishop) on Feb 23, 2016 at 09:23 UTC

    Unfortunately too much of your example is either commented out or are variables which we cannot tell how you have set. An SSCCE would be preferable. Here's one showing how it works:

    #!/usr/bin/env perl use strict; use warnings; my $command = 'echo foo'; my $stdout = `$command`; print "STDOUT: $stdout"; $command = 'ls some-missing-file'; my $stderr = `$command 2>&1 >/dev/null`; print "STDERR: $stderr"; print "\nNow display stdout while command runs:\n"; $command = 'echo bar;sleep 3; echo baz'; open my $pipe, "($command) |"; while (<$pipe>) { print; } close $pipe

    Caveat: only tested on Linux. Differences on Win32 are O/S related and not something I can help you with.

      Sorry for the messy code, the commented out bits are things I've tried and couldn't get working.

      Hopefully this is easier to read?

      Example of command line

      7z.exe a -mx0 -tzip c:\temp\test.zip d:\* -x!$recycle.bin
      our $zip_cmd = '"c:\Program Files\7zip\7z.exe"'; our $sysvolume = '"-x!System Volume Information"'; my $exclude_list = ''; $exclude_list = " -x!*RECYCLE.BIN -x!*Thumbs.db -x!*desktop.ini"; my $command = $zip_cmd . ' a -r -mx0 -tzip' . $exclude_list . ' ' . $s +ysvolume . ' ' . $target_zip . ' '; my $ZIP = undef; open($ZIP,"$command |") or die "$package: Could not open pipe for $com +mand, Error: $!\n"; while (my $line = <$ZIP>) { print $line; #Record in the log file $trace and print STDOUT $line; # Report it on the screen if neede +d } close $ZIP;

      I'll try your examples

Re: Caturing 7zip output with Perl
by Corion (Patriarch) on Feb 24, 2016 at 14:56 UTC

    I wrote (but did not release on CPAN) Archive::SevenZip. This might do enough of what you actually want.

    The module is rough on some parts - I use it mostly to extract files from a ZIP archive on Windows, maybe it is useful to you as well.

    The module and its API certainly could use some love, so if somebody is willing to adopt it or to make its API more compatible with the API of Archive::ZIP, both would be very welcome.

Re: Caturing 7zip output with Perl
by dasgar (Priest) on Feb 23, 2016 at 19:35 UTC

    Perhaps Capture::Tiny and its tee function might be helpful with capturing and displaying the output from the 7zip program.