Closing and re-opening redirected STDOUT

Discussion in 'Perl Misc' started by Noel Sant, Feb 25, 2004.

  1. Noel Sant

    Noel Sant Guest

    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
     
    Noel Sant, Feb 25, 2004
    #1
    1. Advertising

  2. Noel Sant

    Anno Siegel Guest

    Noel Sant <noel.sant(delete this)@ntlworld.com> wrote in comp.lang.perl.misc:

    [...]

    > 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
     
    Anno Siegel, Feb 25, 2004
    #2
    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. Ben_
    Replies:
    0
    Views:
    343
  2. Replies:
    8
    Views:
    5,630
  3. Pappy
    Replies:
    3
    Views:
    391
  4. Replies:
    2
    Views:
    365
    A. Sinan Unur
    Dec 7, 2005
  5. Dilbert
    Replies:
    5
    Views:
    149
    Dilbert
    Jan 24, 2010
Loading...

Share This Page