Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > RE-Redirecting STDOUT

Reply
Thread Tools

RE-Redirecting STDOUT

 
 
aisarosenbaum@yahoo.com
Guest
Posts: n/a
 
      09-02-2004
No I'm not a stuttering typist. ;^)

I'm in the pecular position of working in an environment in which
STDOUT has been rudely redirected by a script (A) that runs my
scripts (B). I want to take STDOUT back without knowing the
handle to which it was redirected. I've found a lot of advise
on redirecting STDOUT, but I need to re-redirect it back to the
console from 'B' without hacking 'A'.

(This is on Solaris)

Any ideas?

Aisa
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      09-02-2004


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> No I'm not a stuttering typist. ;^)
>
> I'm in the pecular position of working in an environment in which
> STDOUT has been rudely redirected by a script (A) that runs my
> scripts (B). I want to take STDOUT back without knowing the
> handle to which it was redirected. I've found a lot of advise
> on redirecting STDOUT, but I need to re-redirect it back to the
> console from 'B' without hacking 'A'.
>
> (This is on Solaris)


On Unix-like OS opening the virual device /dev/tty opens the current
session's controling terminal.

This, of course, has nothing to do with Perl.
>
> Any ideas?
>
> Aisa


 
Reply With Quote
 
 
 
 
aisarosenbaum
Guest
Posts: n/a
 
      09-07-2004
Brian McCauley <(E-Mail Removed)> wrote in message news:<ch7r2j$q1e$(E-Mail Removed)>...
> (E-Mail Removed) wrote:
> > No I'm not a stuttering typist. ;^)
> >
> > I'm in the pecular position of working in an environment in which
> > STDOUT has been rudely redirected by a script (A) that runs my
> > scripts (B). I want to take STDOUT back without knowing the
> > handle to which it was redirected. I've found a lot of advise
> > on redirecting STDOUT, but I need to re-redirect it back to the
> > console from 'B' without hacking 'A'.
> >
> > (This is on Solaris)

>
> On Unix-like OS opening the virual device /dev/tty opens the current
> session's controling terminal.
>
> This, of course, has nothing to do with Perl.


Thanks, this works:

open( STDOUT, ">/dev/tty" );

Any ideas for a MS-portable solution?

Aisa
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      09-08-2004

Quoth (E-Mail Removed) (aisarosenbaum):
> Brian McCauley <(E-Mail Removed)> wrote in message news:<ch7r2j$q1e$(E-Mail Removed)>...
> > (E-Mail Removed) wrote:
> > > No I'm not a stuttering typist. ;^)
> > >
> > > I'm in the pecular position of working in an environment in which
> > > STDOUT has been rudely redirected by a script (A) that runs my
> > > scripts (B). I want to take STDOUT back without knowing the
> > > handle to which it was redirected. I've found a lot of advise
> > > on redirecting STDOUT, but I need to re-redirect it back to the
> > > console from 'B' without hacking 'A'.
> > >
> > > (This is on Solaris)

> >
> > On Unix-like OS opening the virual device /dev/tty opens the current
> > session's controling terminal.
> >
> > This, of course, has nothing to do with Perl.

>
> Thanks, this works:
>
> open( STDOUT, ">/dev/tty" );
>
> Any ideas for a MS-portable solution?


IIRC you can open CON: under MS to write to the current console window. You
could try suggesting to the maintainers of File::Spec that they add this to
that module.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ] (E-Mail Removed)
 
Reply With Quote
 
dan baker
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> No I'm not a stuttering typist. ;^)
>
> I'm in the pecular position of working in an environment in which
> STDOUT has been rudely redirected by a script (A) that runs my
> scripts (B). I want to take STDOUT back without knowing the
> handle to which it was redirected. I've found a lot of advise
> on redirecting STDOUT, but I need to re-redirect it back to the
> console from 'B' without hacking 'A'.
> -------------------


you should be able to CLOSE STDOUT and re-OPEN it to a file or the
screen or wherever you want.

d
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) (dan baker) writes:
> (E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
>> No I'm not a stuttering typist. ;^)
>>
>> I'm in the pecular position of working in an environment in which
>> STDOUT has been rudely redirected by a script (A) that runs my
>> scripts (B). I want to take STDOUT back without knowing the
>> handle to which it was redirected. I've found a lot of advise
>> on redirecting STDOUT, but I need to re-redirect it back to the
>> console from 'B' without hacking 'A'.
>> -------------------

>
> you should be able to CLOSE STDOUT and re-OPEN it to a file or the
> screen or wherever you want.


Well, that's the question, innit? How to figure out what STDOUT used
to be, before the environment redirected it. You can't just open
/dev/tty, because that might not have been where it was pointed. The
only way I can figure is for this environment to provide a global or
package-local variable that holds the original STDOUT, and re-open
STDOUT to it.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      09-08-2004
dan baker wrote:

>>I'm in the pecular position of working in an environment in which
>>STDOUT has been rudely redirected by a script (A) that runs my
>>scripts (B). I want to take STDOUT back without knowing the
>>handle to which it was redirected. I've found a lot of advise
>>on redirecting STDOUT, but I need to re-redirect it back to the
>>console from 'B' without hacking 'A'.

>
> you should be able to CLOSE STDOUT and re-OPEN it to a file or the
> screen or wherever you want.


That is fine for redirecting STDOUT to somewhere else for the duration.

It does not handle the case of temporarily redirecting STDOUT somwhere
else and then re-pointing STDOUT back to where it was going originally.
(It may have been going to a pipe, a file, or something else that was
not connected to the terminal.)
-Joe
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) wrote:

> I'm in the pecular position of working in an environment in which
> STDOUT has been rudely redirected by a script (A) that runs my
> scripts (B). I want to take STDOUT back without knowing the
> handle to which it was redirected. I've found a lot of advise
> on redirecting STDOUT, but I need to re-redirect it back to the
> console from 'B' without hacking 'A'.


perldoc -f open

Look for the section marked 'Here is a script that saves,
redirects, and restores "STDOUT" and "STDERR" using various methods:'.
-Joe
 
Reply With Quote
 
J. Romano
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) (aisarosenbaum) wrote in message news:<(E-Mail Removed). com>...
> > >
> > > I'm in the pecular position of working in an environment in which
> > > STDOUT has been rudely redirected by a script (A) that runs my
> > > scripts (B). I want to take STDOUT back without knowing the
> > > handle to which it was redirected. I've found a lot of advise
> > > on redirecting STDOUT, but I need to re-redirect it back to the
> > > console from 'B' without hacking 'A'.

>
> Thanks, this works:
>
> open( STDOUT, ">/dev/tty" );
>
> Any ideas for a MS-portable solution?



Dear Aisa,

On DOS/Win32 platforms, try:

open(STDOUT, ">CON") or warn $!;

Hope this helps (and works!).

-- Jean-Luc
 
Reply With Quote
 
Scott W Gifford
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) (dan baker) writes:

> (E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
>> No I'm not a stuttering typist. ;^)
>>
>> I'm in the pecular position of working in an environment in which
>> STDOUT has been rudely redirected by a script (A) that runs my
>> scripts (B). I want to take STDOUT back without knowing the
>> handle to which it was redirected. I've found a lot of advise
>> on redirecting STDOUT, but I need to re-redirect it back to the
>> console from 'B' without hacking 'A'.
>> -------------------


On my Solaris system at least, the original STDOUT filehandle is lost
when a new one is opened. The original filehandle is on file
descriptor 1, and when STDOUT is opened to a new place, that file
descriptor is replaced:

$ truss perl -e'open(STDOUT,"> /tmp/test")'
...
open64("/tmp/test", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
fstat64(3, 0x00124D40) = 0
fcntl(3, F_DUP2FD, 0x00000001) = 1
close(3) = 0
...

(use strace instaed of truss on a Linux system)

That fcntl is a dup2 operation, which closes filehandle 1, then makes
it a copy of file descriptor 3 (which points to /tmp/test).

That means that it's not possible, as far as I can tell, unless you
can modify (A)'s behavior, for example by overloading the open
function before it starts, or save the original STDOUT filehandle,
perhaps from a BEGIN block.

Good luck!

---ScottG.
 
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
How to read STDOUT from a Perl function Alfred von Campe Perl 4 01-05-2009 09:00 AM
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
Writing to stdout in VHDL moshezsb VHDL 9 09-22-2004 04:31 PM



Advertisments