Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: tar, system() & pipes?

by Anonymous Monk
on Jul 17, 2007 at 20:35 UTC ( #627123=note: print w/replies, xml ) Need Help??

in reply to Re: tar, system() & pipes?
in thread tar, system() & pipes?

Thank you for your reply.

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

Replies are listed 'Best First'.
Re^3: tar, system() & pipes?
by ikegami (Patriarch) on Jul 17, 2007 at 20:38 UTC

    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?
        Why is quotemeta needed or why do you have to call 'system' this way? For quotemeta the code will work for files with special characters in them. For system I imagine the LIST version exists to make it easier to pass in a large number of parameters, deal with ones with special characters, and/or build a list of parameters without having to join them later on. As explained by others the IO redirection ('>') is something the shell handles but when system is called in LIST context it will be passed to the tar command as a parameter instead. Because tar has no '>' command line option it errors out -- just like it does with any other incorrect parameter (e.g. -y).
Re^3: tar, system() & pipes?
by BrowserUk (Patriarch) on Jul 17, 2007 at 20:46 UTC

    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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://627123]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-03-30 23:12 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.