in reply to Another system redirect problem

As an anonymous monk explained, you can't execute a shell command (of which ">>" is a part) without running a shell!

IPC::Open3 is a somewhat low-level way of doing it, but it's an option.

use IPC::Open3 qw( open3 ); open(local *OUT_FH, '>>', '/Volumes/Expansion Drive/stuffTGZ/list_xxx_ +dirs.txt') or die $!; print(OUT_FH "-------------------------------------------------\n"); print(OUT_FH "- $File::Find::name -----------------------------\n"); # Gets closed by open3. open(local *CHILD_STDIN, '<', '/dev/null') or die $!; my $pid = open3( '<&CHILD_STDIN', '>&OUT_FH', '>&STDERR', "/bin/ls" => ( "-l", $File::Find::name ), ); waitpid($pid, 0);

You can reuse OUT_FH for multiple calls to open3, but you need to reopen CHILD_STDIN each time.

Replies are listed 'Best First'.
Re^2: Another system redirect problem
by Anonymous Monk on Jun 18, 2011 at 06:43 UTC
    I believe IPC::Run provides a solution
    # Can do I/O to sub refs and filenames, too: run \@cmd, '<', "in.txt", \&out, \&err or die "cat: $?" run \@cat, '<', "in.txt", '>>', "out.txt", '2>>', "err.txt";

      From what I gathered from the docs and a lot of testing, it would look like

      use IPC::Run qw( run ); open(my $fh, '>>', '/Volumes/Expansion Drive/stuffTGZ/list_xxx_dirs.tx +t') or die $!; print($fh "-------------------------------------------------\n"); print($fh "- $File::Find::name -----------------------------\n"); run [ "/bin/ls" => ( "-l", $File::Find::name ) ], '<', \"", '>', $fh;

      Notes:

      • Lexical file handles work, but I didn't see it in the documentation.
      • «'<', \""» appears to open /dev/null (desirable) as opposed to closing STDIN as documented (undesirable).
      • The child appears to inherit the parent's STDERR if 2> isn't specified. That's probably documented, but I didn't see it.