RE-Redirecting STDOUT

A

aisarosenbaum

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
 
B

Brian McCauley

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

aisarosenbaum

Brian McCauley said:
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
 
B

Ben Morrow

Quoth (e-mail address removed) (aisarosenbaum):
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
 
D

dan baker

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
 
E

Eric Schwartz

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
 
J

Joe Smith

dan said:
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
 
J

Joe Smith

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
 
J

J. Romano

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
 
S

Scott W Gifford

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.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top