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


in reply to tar, system() & pipes?

The > /tmp/file is a *shell* directive, but the shell is not invoked when you use system LIST instead of system EXPR. You're passing > and /tmp/file as arguments to tar, and tar doesn't know what to do with them.

Replies are listed 'Best First'.
Re^2: tar, system() & pipes?
by Anonymous Monk on Jul 17, 2007 at 20:35 UTC

    Thank you for your reply.

    So there isn't a way to spawn piped commands from within a Perl script?

      I think the following is safe on unix platforms:

      my $q_archive = quotemeta($ARGV[0]); my $q_filename = quotemeta($filename); system("tar $args $q_archive > $q_filename")

      It can also be written as

      system("tar $args \Q$ARGV[0]\E > \Q$filename\E")

      quotemeta (called directly or via \Q..\E) will backslash-escape any non-word characters, so the file names can safely contain symbols and spaces.

        Thank you! Use of quotemeta appears to work.

        Why is this needed?

      If the intention is to read the output file back into your program once tar is finished, you could bypass the filesystem by using a piped-open:

      (See the docs for perlfuncopen and perlopentut)

      # ..................v open TAR, "tar .... |" or die ...; while( <TAR> { ## do stuff with the tar output }

      ## do stuff with the tar output could be as simple as print FILE $_;.

      See also IO::Pipe.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.