writing to terminal even with STDOUT and STDERR redirected

D

Dilbert

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;
 
M

Martijn Lievaart

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
 
I

Ilya Zakharevich

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
 
C

C.DeRykus

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";
 
C

C.DeRykus

Quoth "C.DeRykus" <[email protected]>:


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

Dilbert

On 22 jan, 23:20, "Mumia W." <paduille.4061.mumia.w
(e-mail address removed)> wrote:

Thanks to everyone who replied, there are a lot of good suggestions I
can go through for my problem.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top