Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to tar, system() & pipes? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-20 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found