Redirecting stdout to a file in perl


















I'm looking for an example of redirecting stdout to a file using Perl. As JS Bangs said, an easy way to redirect output is to use the 'select' statement. Many thanks to stackoverflow and their users. I hope this is helpful. The best the parent can do is use system or exec or something of the sort to do shell redirection. Though there's almost certainly a more elegant way of going about this depending on the exact details of what you're trying to do, if you absolutely must have dup2 , its Perl equivalent is present in the POSIX module.

However, in this case you're dealing with actual file descriptors and not Perl filehandles, and correspondingly you're restricted to using the other provided functions in the POSIX module, all of which are analogous to what you would be using in C.

To some extent, you would be writing C in very un-Perlish Perl. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How can I redirect standard output to a file in Perl? If order of output must be: stdout then stderr ; there is no solution with redirection only. The stderr must be stored to a temporal file.

The only way to redirect one output an fd like stdout or stderr to two files is to reproduce it. The command tee is the correct tool to reproduce a file descriptor content. So, an initial idea to have one output on two files would be to use:. But we need to append use -a and only need one copy. This solve both issues:.

To supply tee with stdout the one to repeat we need to consume stderr directly out of the command. One way, if order is not important order of output will most probably be preserved as generated, whichever is output first will be stored first.

Option 2 works only in some shells. Option 3 uses an additional subshell slower but use the file names only once. But if stdout must be first whichever order output is generated we need to store stderr to append it to file at the end first solution posted. The standard output of the cmd is sent to both the stdout and the stderr of the pipeline.

The standard error of the cmd bypasses the tee and comes out the stderr of the pipeline. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Ask Question. Asked 2 years, 8 months ago. Active 2 years, 8 months ago.

As a user, without looking inside the code, you can separate the two channels: If you run perl program. So on the screen you will see only the content of the Standard Error: Could not open file If you open the out. Redirecting Standard Error On the other hand if you run the script as perl program. On the screen you will see this: Welcome to our little program If you open the err.

Redirecting both You can even redirect both channels at the same time using both symbols on the command line. Running the script as perl program. All the content printed to the standard output channel will be in the out. In the above example, the files names out.

You can use any names there. It behaves like a black hole. Whatever is printed to that file will disappear without any trace. The main use of this is when there is a program and the user wants to throw away either the regular output or the error messages. For example, you might have an application, one that you cannot change, that spit tons of messages to the standard error channel.

If you don't want to see that on the screen you can redirect it to a file. But if you do that, it can fill your disk quickly.



0コメント

  • 1000 / 1000