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

后端 未结 5 1628
青春惊慌失措
青春惊慌失措 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:10

    open my $fh, '>', $file;
    defined(my $pid = fork) or die "fork: $!";
    if (!$pid) {
        open STDOUT, '>&', $fh;
    
        # do whatever you want
        ...
    
        exit;
    }
    waitpid $pid, 0;
    print $? == 0 ? "ok\n" : "nok\n";
    

提交回复
热议问题