Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Opening and Closing STDOUT

by Anonymous Monk
on Mar 05, 2003 at 04:22 UTC ( [id://240525]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

In a script I'm working on I had to use the following code:

open STDOUT, ">$tempfile"; #do some stuff here close STDOUT;
I had to do so because the stuff I was doing in between the "open" and "close" statements used a module that spat out a lot of chatter to STDOUT, but I didn't want it to appear on the console when the script was run. The only problem is that after doing this, nothing would print to STDOUT from print statements further down the script! E.g., if I did the above and then followed with:

print "Gee, this is fun!\r\n";

And then ran the script, nothing would appear on my console...I wouldn't see "Gee, this is fun!".

Anyone know how I can fix this (e.g. how I can "reopen" STDOUT to whatever it was before I messed with it)?

Replies are listed 'Best First'.
Re: Opening and Closing STDOUT
by chromatic (Archbishop) on Mar 05, 2003 at 04:44 UTC

    localize the typeglob *STDOUT that contains the STDOUT filehandle within a block, then call your chatty code within the block:

    { local *STDOUT; # call something that prints to STDOUT }

      ++chromatic , certainly localizing *STDOUT fixes the problem, but just to play devils advocate..., IS it possible to close STDOUT and re-open it again to the console?

      print "Standard and Out\n"; close STDOUT; open (STDOUT, ">>over.txt") or die "Screaming $!"; print "Over and Out\n"; close STDOUT; open (STDOUT, ">>/dev/pts/0") or die "Can open console $!" #Ok so I ch +eated and used `who` to find this print "Aye Aye Capt'n\n";

      Which of course is largely useless and redundant given your solution, plus I have NFI how to programatically determine which /dev to open


      food for thought gives me indigestion

        Surely what you want to do is to duplicate the STDOUT file handle to keep it out of the way. Then duplicate it back to STDOUT when you are done. Something like:

        local(*SAVED_STDOUT); open(SAVED_STDOUT,">&STDOUT"); ...Mess with STDOUT... close(STDOUT); open(STDOUT,">&SAVED_STDOUT");

        Warning: I am not near a machine where I can check this at the moment, so this is probably not be accurate, look in your favourite O'Reilly book to get the real syntax.

Re: Opening and Closing STDOUT
by graff (Chancellor) on Mar 05, 2003 at 05:02 UTC
    I haven't hit on the correct answer (yet), but in the meantime, you might consider using STDERR for the part that follows the use of that (ugly) module. Since you're not redirecting STDERR, it will continue to show on the console; maybe you won't really need STDOUT at that point.

    update:Aha! chromatic nailed it (I'm still working on my typeglob skills...) -- here's a working example:

    print "We are about to go into that ugly module...\n"; { local *STDOUT; open( STDOUT, ">/tmp/junk.stdout" ); print "This goes to junk.stdout\n"; } print "Okay, that mess is over with.\n";
    You'll see that only the first and third prints go to the console, and you can check the contents of /tmp/junk.stdout to confirm where the middle print went.
Use select to redirect STDOUT (Was Re: Opening and Closing STDOUT)
by hiseldl (Priest) on Mar 05, 2003 at 14:23 UTC

    Instead of closing and opening STDOUT, you could use select as follows:

    open(JUNK, ">junk.txt") or die; print "this is going to stdout\n"; select JUNK; print "this is going to junk\n"; select STDOUT; print "this is again going to stdout\n";

    This way you can leave your filehandles open and switch whenever you need to with little effort.

    HTH.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: Opening and Closing STDOUT
by nite_man (Deacon) on Mar 05, 2003 at 10:03 UTC
    You can use a following code:
    # Redirect STDOUT to your file open(TMP, ">&STDOUT); open(STDOUT, ">/tmp/out.tmp") or die "Can't open a temporary file: $!" +; # do something, all programm input will be redirected to your tmp # file exept error messages of course. # Restore STDOUT open(STDOUT, ">&TMP"); close(TMP) or die "Can't close temporary file: $!";


    { firstname => 'Michael', quote => 'Mice were crying and stinging but went on nibbling the + cactus', }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://240525]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found