http://qs1969.pair.com?node_id=627115

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

I'm needed to do further processing on the contents of a tar file. Manually, I'm simply issue:

$ tar txf syssrc.tgz > /tmp/file
...to create a list of directories & files, but doing this programmatically within a Perl script is giving me problems.

If I don't redirect, the following code works:

#!/usr/bin/perl use strict; use warnings; use Readonly; Readonly my $filename => '/tmp/contents'; my $args = 'tf'; eval { die 'incorrect #command-line arguments' unless @ARGV == 1; # add switches based on filetype if ($ARGV[0] =~ /tgz$/) { $args .= 'z'; } # dump tar contents to file if (system('tar', $args, $ARGV[0]) != 0) { if ($? == -1) { die "failed to execute '$!'"; } elsif ($? & 127) { die sprintf 'child process died with signal %d %s', ($? & 127), (($? & 128) ? 'with coredump' : ''); } else { printf "child process exit value:\t%d\n", ($? >> 8); } } }; if ($@) { $@ =~ s/at $0 line \d+\.$//; print "syntax:\t$0 <filename>\n"; print "error:\t$@\n"; } ..but if I change the <b>system()</b> call to: <code> if (system('tar', $args, $ARGV[0], '>', $filename) != 0) {
I get the following warning:
tar: WARNING! These patterns were not matched: > /tmp/contents child process exit value: 1
...and /tmp/file is not created.