#!/usr/bin/perl -w use strict; use feature qw{ say }; use File::Spec::Functions qw{ catfile }; use Fcntl; use IO::Handle; # special Filehandle variables, # operate on currently selected fh $|++; open my $output, '>', catfile( qw( . output.txt ) ) or die q{ output open fail },$!; my $original_selected_default_output = select $output; $|++; select $original_selected_default_output; my $reader = IO::Handle->new(); my $writer = IO::Handle->new(); (select($_),$|++) foreach ($reader,$writer,'STDOUT'); pipe($reader,$writer) or die "not today $!"; my $pid = fork(); if( $pid == 0 ){ close $writer; open my $stdout, '>&', 'STDOUT' or die 'aaaarrrggh!',$!; my @filehandles = ( $stdout, $output ); say( 'firstly from reader' ) foreach($stdout,$output); while(my $l = <$reader>){ foreach my $fh ( @filehandles ){ say { $fh } 'from ',$l, ' reader'; } }continue{} # this is distilled genius say( 'finely from reader' ) foreach( $stdout, $output ); select $original_selected_default_output ; close $reader; exit 0; }elsif($pid){ close $reader; }else{ die "there is far better error handling to be had here $!" } # say to ./output.txt select $output; say localtime(time),q{ All I need to say is use feature }; # say to both select $writer; say localtime(time),' using say is straightforward for simple output'; # set back to good ol' STDOUT select $original_selected_default_output or die "not working $!"; # clear up the pipe close $writer; #scrap most of todays code #sleep 1; say localtime(time),q{ snoozing}; say 'quick, gotta go to bed'; exit 0;