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

后端 未结 5 1627
青春惊慌失措
青春惊慌失措 2020-12-17 18:07

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 f

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 18:31

    A strictly informational but impractical answer:

    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.

    http://perldoc.perl.org/POSIX.html

提交回复
热议问题