in reply to Re: How to print STDOUT to a file
in thread How to print STDOUT to a file
calls the shell interpreter under the hood, something that can be very inconvenient (and even dangerous). For instance, the script arguments have to be properly quoted or they could be wrongly splitted by the shellsystem("script.sh >> filename");
An alternative way to redirect the output for the called program is to reopen STDOUT:
Or on Unix/Linux systems, you can use fork and exec instead of system and perform the redirection after the fork:open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!"; open STDOUT, ">", $fn or die "Can't open $fn: $!"; system $script, @args; open STDOUT, ">&", $oldout;
my $pid = fork; if (defined $pid and $pid == 0) { open STDOUT, ">", $fn or die; exec $script, @args; exit 1; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: How to print STDOUT to a file
by Anonymous Monk on Nov 14, 2005 at 12:17 UTC |