Closing and re-opening redirected STDOUT

N

Noel Sant

Hi! I have two questions:

The first is rather silly and off-topic. I have posted questions here a
couple of times, and have always been given good answers and advice. I have
then tried to reply to my original post with a thank-you message, but it
never seems to get there. I can see my original post fairly soon after I
send it off, but never my reply. I'm using MS Outlook Newsreader (Help/About
says it's part of Outlook Express 5), and when I reply it asks whether I
want to reply to the group or an individual, and I choose group. Can anyone
guess what's wrong? Is there a better newsreader that I should use?

Second question: I have a program whose STDOUT I redirect to a file, with a
command like:
D:\>perl -w myprog.pl config_file >> log_file.txt
This on a Windows 2000 system, using ActiveState's Perl.

In the program I call WinZip, sometimes to zip stuff that includes this log
file. WinZip then objects because it's open. Well, actually it just warns
me, and says that if I write to it, it could corrupt the zipped file. So I
want to close STDOUT before calling WinZip, and re-open it afterwards. Is
this possible? When I try, Perl gets very upset, but I suspect that may be
because I don't know, within the program, what the file name is to which
it's been redirected. How can I find this out? Or is there some way to tell
Perl to use the one it had before?

TIA,

Noel Sant
 
A

Anno Siegel

[...]
Second question: I have a program whose STDOUT I redirect to a file, with a
command like:
D:\>perl -w myprog.pl config_file >> log_file.txt
This on a Windows 2000 system, using ActiveState's Perl.

In the program I call WinZip, sometimes to zip stuff that includes this log
file.

Normally, a program has no business compressing its own log file. The
compressing program should deal with the situation that it can't properly
access the file, and that's it. Think of a redesign that avoids this
situation, or postpones the compression, or whatever.
WinZip then objects because it's open. Well, actually it just warns
me, and says that if I write to it, it could corrupt the zipped file. So I
want to close STDOUT before calling WinZip, and re-open it afterwards. Is
this possible?

Are you saying you want to compress a file, then re-open it as the log
file? Please say it ain't so.
When I try, Perl gets very upset,

So what did you try? We can't correct code we don't get to see!
but I suspect that may be
because I don't know, within the program, what the file name is to which
it's been redirected. How can I find this out?

You can't. There can be zero to many file names that correspond to a given
filehandle. There is no way of telling which (if any) was used to open the
file.

[Side note: That specific question "I have a file handle. What's the
file name?" should have a FAQ entry, Perl question or no]
Or is there some way to tell
Perl to use the one it had before?

Yes, that's possible. See "perldoc -f open" and look for the string "&=N".
Before investing more time, re-think what you are trying to do. It doesn't
seem to make sense.

Anno
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top