Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 933721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:52:51+00:00 2026-05-15T20:52:51+00:00

In perl, after fork() ing I can redirect a child’s stdout to a file

  • 0

In perl, after fork()ing I can redirect a child’s stdout to a file like so

open STDOUT,">",$filename or die $!

I’m wondering if there is a way of “copying it”, keeping the stdout on the parent’s stdout but also copying to a specified file. It should happen in a way that would not require any buffering and the user would see the console output in real time. This would sort of be like the unix tee. But ideally the solution would not involve any third party libraries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:52:53+00:00Added an answer on May 15, 2026 at 8:52 pm

    In the child, execute

    open STDOUT, "|-", "tee", $output
      or die "$0: failed to start tee: $!";
    

    If you don’t want to use tee for some reason, you could use a poor man’s version by forking another child via open STDOUT, "|-" and doing the duplication in the grandchild:

    #! /usr/bin/perl
    
    use warnings;
    use strict;
    
    my $pid = fork;
    die "$0: fork: $!" unless defined $pid;
    
    if ($pid) {
      waitpid $pid, 0;
    }
    else {
      my $pid = open STDOUT, "|-";
      die "$0: fork: $!" unless defined $pid;
      select STDOUT; $| = 1;  # disable STDOUT buffering
      if ($pid) {
        print "Hiya!\n";
        system "echo Howdy";
        close STDOUT or warn "$0: close: $!";
        exit 0;
      }
      else {
        open my $fh, ">", "/tmp/other-output" or die "$0: open: $!";
        my $status;
        while ($status = sysread STDIN, my $data, 8192) {
          syswrite $fh, $data and print $data or die "$0: output failed: $!";
        }
        die "$0: sysread: $!" unless defined $status;
        close $fh or warn "$0: close: $!";
        exit 0;
      }
    }
    

    Sample run:

    $ ./owntee 
    Hiya!
    Howdy
    
    $ cat other-output 
    Hiya!
    Howdy
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I stop my Perl program from closing the window after finishing execution
After studying the Perl require docs and other links like this on Stackoverflow I'm
Perl's WWW::Mechanize returns complete garbage (like ðäD=°lc*Ñ2\7 õä1û¼Âc{oî´lDNöÓ8ß5o*F2Õ©-õ£z§2ÜÝ1,³íäþwɯÓQÞÆÁS¿IZDKÁ»,ËmÅS1r4!°s,¥4Jl;\J~í2¼) after retrieving a page. I am
After trying my hand at Perl and a little bit of C, I am
Perl is really good for writing the kind of string/file parsing programs that I
In Perl, there is an ability to break an outer cycle like this: AAA:
I just mangaged to install the perl/Tk module after much struggle. I realise I
After executing these lines in Perl: my $data = `curl '$url'`; my $pets =
I have Perl program based on IO::Async , and it sometimes just exits after
I came to know PHP after Perl, so when I first found preg_* function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.