in reply to No inherited file handles with tie() and exec()

The exec'ed process /bin/date knows nothing about Perl file handles. It always prints to file descriptor 1 (stdout).  In your last case where you're redirecting the good-old fashioned way, the file descriptor associated with Perl's STDOUT is still 1 after the redirection. OTOH, the descriptor associated with the file handle $fh (opened within TIEHANDLE) isn't 1. (Try fileno to verify this yourself.)

Update: the descriptor's "close-on-exec" flag (FD_CLOEXEC) also plays a role here.  By default it is on (i.e. do close) for everything but descriptors 0-2. But you could clear the flag, so the descriptor associated with $fh would survive the exec. In this case, you could then redirect /bin/date's output to that inherited file descriptor (typically number 3 or 4):

#!/usr/bin/perl tie *STDOUT, TrapClass; my $pid = fork(); die "" if !defined $pid; exit 0 if $pid; # parent exits my $tied_descr = fileno(STDOUT); exec "/bin/date >&$tied_descr"; # child package TrapClass; use Fcntl qw(F_GETFD F_SETFD FD_CLOEXEC); sub TIEHANDLE { my $class = shift; open(my $fh, ">out") or die; my $flags = fcntl($fh, F_GETFD, 0) or die "Can't get flags: $!\n"; # clear close-on-exec flag fcntl($fh, F_SETFD, $flags & ~FD_CLOEXEC) or die "Can't set flags: + $!\n"; bless { fh => $fh }, $class; } sub PRINT { my $self = shift; my $fh = $self->{fh}; print $fh @_; } sub FILENO { my $self = shift; my $fh = $self->{fh}; return fileno($fh); }

As desired, the output of /bin/date would end up in the file 'out' that you've tied STDOUT to. However, this is not happening via Perl's tied subroutine calls, but rather via the child directly writing to the associated file descriptor...