Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > writing to terminal even with STDOUT and STDERR redirected

Reply
Thread Tools

writing to terminal even with STDOUT and STDERR redirected

 
 
Dilbert
Guest
Posts: n/a
 
      01-22-2010
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;
 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-22-2010
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
 
Reply With Quote
 
 
 
 
Ilya Zakharevich
Guest
Posts: n/a
 
      01-23-2010
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
 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      01-23-2010
On Jan 22, 9:31*am, Dilbert <dilbert1...@gmail.com> 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


 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      01-23-2010
On Jan 22, 5:13*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "C.DeRykus" <dery...@gmail.com>:
>
>
> ...


>
> 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
 
Reply With Quote
 
Dilbert
Guest
Posts: n/a
 
      01-24-2010
> On Jan 22, 9:31*am, Dilbert <dilbert1...@gmail.com> 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 <m...@rtij.nl.invlalid> wrote:
On 22 jan, 23:20, "Mumia W." <paduille.4061.mumia.w
+nos...@earthlink.net> wrote:
On 23 jan, 01:27, Ilya Zakharevich <nospam-ab...@ilyaz.org> wrote:
On 23 jan, 01:46, "C.DeRykus" <dery...@gmail.com> wrote:
On 23 jan, 02:13, Ben Morrow <b...@morrow.me.uk> wrote:

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to STDOUT after closing controlling terminal Matt Bleh Ruby 6 09-22-2009 12:39 AM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Closing and re-opening redirected STDOUT Noel Sant Perl Misc 1 02-25-2004 05:53 PM
Retrieve the directory where StdOut was redirected Ben_ Java 0 10-30-2003 02:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57