RE-Redirecting STDOUT

Discussion in 'Perl Misc' started by aisarosenbaum@yahoo.com, Sep 2, 2004.

  1. Guest

    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
     
    , Sep 2, 2004
    #1
    1. Advertising

  2. 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
     
    Brian McCauley, Sep 2, 2004
    #2
    1. Advertising

  3. Brian McCauley <> wrote in message news:<ch7r2j$q1e$>...
    > 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
     
    aisarosenbaum, Sep 7, 2004
    #3
  4. Ben Morrow Guest

    Quoth (aisarosenbaum):
    > Brian McCauley <> wrote in message news:<ch7r2j$q1e$>...
    > > 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. ]
     
    Ben Morrow, Sep 8, 2004
    #4
  5. dan baker Guest

    wrote in message news:<>...
    > 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
     
    dan baker, Sep 8, 2004
    #5
  6. (dan baker) writes:
    > wrote in message news:<>...
    >> 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.
     
    Eric Schwartz, Sep 8, 2004
    #6
  7. Joe Smith Guest

    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
     
    Joe Smith, Sep 8, 2004
    #7
  8. Joe Smith Guest

    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
     
    Joe Smith, Sep 8, 2004
    #8
  9. J. Romano Guest

    (aisarosenbaum) wrote in message news:<>...
    > > >
    > > > 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
     
    J. Romano, Sep 8, 2004
    #9
  10. (dan baker) writes:

    > wrote in message news:<>...
    >> 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.
     
    Scott W Gifford, Sep 8, 2004
    #10
    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. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,265
    Joe Smith
    May 13, 2004
  2. Sudsy
    Replies:
    0
    Views:
    4,408
    Sudsy
    Jul 18, 2003
  3. Douwe
    Replies:
    1
    Views:
    5,716
    Mike Schilling
    Jan 12, 2004
  4. Alexander Stippler

    redirecting stdout to ostream

    Alexander Stippler, Feb 5, 2004, in forum: C++
    Replies:
    4
    Views:
    7,348
    Alexander Stippler
    Feb 6, 2004
  5. Elad
    Replies:
    0
    Views:
    433
Loading...

Share This Page