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...


In reply to Re: No inherited file handles with tie() and exec() by almut
in thread No inherited file handles with tie() and exec() by saintmike

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.