Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I redirect standard output to a file in Perl?

I'm looking for an example of redirecting stdout to a file using Perl. I'm doing a fairly straightforward fork/exec tool, and I want to redirect the child's output to a file instead of the parents stdout.

Is there an equivilant of dup2() I should use? I can't seem to find it

like image 232
Mike Avatar asked May 25 '10 18:05

Mike


People also ask

How do I redirect Perl output to a file?

Redirect STDOUT using a filehandle As with select, this will have a global affect on the Perl program. use feature qw/say/; use autodie; # copy STDOUT to another filehandle open (my $STDOLD, '>&', STDOUT); # redirect STDOUT to log. txt open (STDOUT, '>>', 'log. txt'); say 'This should be logged.

How do I redirect standard output to a file?

Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator.

How do I redirect STD output?

The regular output is sent to Standard Out (STDOUT) and the error messages are sent to Standard Error (STDERR). When you redirect console output using the > symbol, you are only redirecting STDOUT. In order to redirect STDERR, you have to specify 2> for the redirection symbol.


1 Answers

From perldoc -f open:

open STDOUT, '>', "foo.out"

The docs are your friend...

like image 185
ivans Avatar answered Sep 21 '22 15:09

ivans