writing to terminal even with STDOUT and STDERR redirected

Discussion in 'Perl Misc' started by Dilbert, Jan 22, 2010.

  1. Dilbert

    Dilbert Guest

    I have a first perl-program ("first.pl") that calls a second perl-
    program ("second.pl") and captures STDOUT and STDERR.
    Is there a way inside "second.pl" to magically open a filehandle to
    the terminal, even though STDOUT and STDERR are both redirected ?
    I'm working under Ubuntu Linux.

    first.pl
    ======
    use strict;
    use warnings;
    system 'perl second.pl >file1.txt 2>file2.txt';

    second.pl
    ======
    use strict;
    use warnings;
    print STDOUT "This is STDOUT\n";
    print STDERR "This is STDERR\n";
    open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
    terminal because $!";
    print {$fh} "This is magically going to terminal\n";
    close $fh;
    Dilbert, Jan 22, 2010
    #1
    1. Advertising

  2. On Fri, 22 Jan 2010 09:31:46 -0800, Dilbert wrote:

    > I have a first perl-program ("first.pl") that calls a second perl-
    > program ("second.pl") and captures STDOUT and STDERR. Is there a way
    > inside "second.pl" to magically open a filehandle to the terminal, even
    > though STDOUT and STDERR are both redirected ? I'm working under Ubuntu
    > Linux.
    >
    > first.pl
    > ======
    > use strict;
    > use warnings;
    > system 'perl second.pl >file1.txt 2>file2.txt';
    >
    > second.pl
    > ======
    > use strict;
    > use warnings;
    > print STDOUT "This is STDOUT\n";
    > print STDERR "This is STDERR\n";
    > open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
    > terminal because $!";
    > print {$fh} "This is magically going to terminal\n"; close $fh;


    open my$fh, ">", "/dev/tty" or die ...

    M4
    Martijn Lievaart, Jan 22, 2010
    #2
    1. Advertising

  3. On 2010-01-22, Dilbert <> wrote:
    > I have a first perl-program ("first.pl") that calls a second perl-
    > program ("second.pl") and captures STDOUT and STDERR.
    > Is there a way inside "second.pl" to magically open a filehandle to
    > the terminal, even though STDOUT and STDERR are both redirected ?


    perldoc Term::ReadLine;

    > I'm working under Ubuntu Linux.


    Does not matter...

    Hope this helps,
    Ilya
    Ilya Zakharevich, Jan 23, 2010
    #3
  4. Dilbert

    C.DeRykus Guest

    On Jan 22, 9:31 am, Dilbert <> wrote:
    > I have a first perl-program ("first.pl") that calls a second perl-
    > program ("second.pl") and captures STDOUT and STDERR.
    > Is there a way inside "second.pl" to magically open a filehandle to
    > the terminal, even though STDOUT and STDERR are both redirected ?
    > I'm working under Ubuntu Linux.
    >
    > first.pl
    > ======
    > use strict;
    > use warnings;
    > system 'perl second.pl >file1.txt 2>file2.txt';
    >
    > second.pl
    > ======
    > use strict;
    > use warnings;
    > print STDOUT "This is STDOUT\n";
    > print STDERR "This is STDERR\n";
    > open my $fh, '>', ??magic-terminal?? or die "Error: Can't open magic
    > terminal because $!";
    > print {$fh} "This is magically going to terminal\n";
    > close $fh;



    A convoluted but more Unix-y portable option
    is to dup STDOUT and undo the close-on-exec
    flag before the system call and then pass
    the dup'ed fd in:


    first.pl
    ========
    use strict;
    use warnings;
    use Fcntl qw/F_SETFD/;

    open( SAVEOUT, ">&STDOUT" ) or die $!;
    fcntl( SAVEOUT, F_SETFD, 0 )
    or die "Can't clear close-on-exec flag on SAVEOUT",": $!";

    system "perl second.pl " . fileno(SAVEOUT) . " "
    . fileno(SAVEOUT) . " >&2"
    . " >file1.txt 2>file2.txt";


    second.txt
    ==========
    use strict;
    use warnings;

    my $savefd = shift; # get STDOUT dup fd

    print STDOUT "This is STDOUT\n";
    print STDERR "This is STDERR\n";

    open(SAVEOUT, "+<&=$savefd") or die "can't re-open SAVEOUT: $!";
    print SAVEOUT "This is magically going to terminal\n";

    --
    Charles DeRykus
    C.DeRykus, Jan 23, 2010
    #4
  5. Dilbert

    C.DeRykus Guest

    On Jan 22, 5:13 pm, Ben Morrow <> wrote:
    > Quoth "C.DeRykus" <>:
    >
    >
    > ...


    >
    > Since /dev/tty is one of only four files mentioned by name in POSIX,
    > it's probably safe to rely on it being there :).
    >


    <off-topic>

    Suprisingly, I didn't find it when I was logged into FreeBSD.
    which led me down the path of convolution. Of course, it's
    a free FreeBSD shell account...maybe that's significant.

    </off-topic>

    --
    Charles DeRykus
    C.DeRykus, Jan 23, 2010
    #5
  6. Dilbert

    Dilbert Guest

    > On Jan 22, 9:31 am, Dilbert <> wrote:
    > > Is there a way inside "second.pl" to magically open a filehandle to
    > > the terminal, even though STDOUT and STDERR are both redirected ?
    > > I'm working under Ubuntu Linux.


    On 22 jan, 21:49, Martijn Lievaart <> wrote:
    On 22 jan, 23:20, "Mumia W." <paduille.4061.mumia.w
    > wrote:
    On 23 jan, 01:27, Ilya Zakharevich <> wrote:
    On 23 jan, 01:46, "C.DeRykus" <> wrote:
    On 23 jan, 02:13, Ben Morrow <> wrote:

    Thanks to everyone who replied, there are a lot of good suggestions I
    can go through for my problem.
    Dilbert, Jan 24, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben_
    Replies:
    0
    Views:
    325
  2. Replies:
    8
    Views:
    5,581
  3. Matt Bleh
    Replies:
    6
    Views:
    150
    David Masover
    Sep 22, 2009
  4. Noel Sant

    Closing and re-opening redirected STDOUT

    Noel Sant, Feb 25, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    115
    Anno Siegel
    Feb 25, 2004
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page