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

Dear Monks,

in my program i use few threads parallel (use threads). For all the threads i uses one error log file (or one debug file), which is closed and open at the midnight by one of the threads (always the same thread).

I read a bit how to share files between functions and some other documents, since i am a beginner in perl it was hard for me to understand them and to know if those are useful articles for my problem or not.

My question is how could i tell the other thread to "update" the pointer to the error log file after i close it and open new file in one of the threads.

Regards,

Replies are listed 'Best First'.
Re: perl file handle share thread
by BrowserUk (Patriarch) on May 20, 2010 at 15:30 UTC

    What makes you think you need to do anything special?

    The following starts a thread logging to STDERR (which is redirected to 'oldLog') once per second. The main thread then sleeps, logs once, reopens STDERR to 'aNewLog'; logs once more then sleeps 3 before ending. You'll see that there is output from both threads in 'oldLog', and also from both in 'aNewLog':

    C> perl -Mthreads -E" async{ warn threads->tid while sleep 1}; sleep 1; warn threads->tid; open STDERR, '>', 'aNewLog'; warn threads->tid; sleep 3 " 2>oldLog C> type oldLog 1 at -e line 1. 0 at -e line 1. C> type aNewLog 0 at -e line 1. 1 at -e line 1. 1 at -e line 1. 1 at -e line 1. Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Dear Monks,

      maybe i was not clear enough, what i ment by error or debug files, was not perl code error messages, rather then some information that i defines as problem, for future improving.

      i would like to know how could i open and close this file in one thread and it will affect all the other threads that are writing to this file handle. (each day i create new error log file, in perl, and all the threads write to this file handle their messages. The day after they will all write to another file handle.

      regards
        i would like to know how could i open and close this file in one thread and it will affect all the other threads that are writing to this file handle.

        That is exactly what my posted code showed you how to do. If all your threads are using a well-known handle (like STDERR) for logging, then all you need to do is re-open that to the new file and all the other threads will be redirected to that new file. (You should also use some locking for safety!)

        If you are using some kind of object-based wrapper or (dratted) singleton instance to obtain or supply the handle, then you will have to post a (small) working snippet that demonstrates what you are doing. We can then look at what needs to be done to make that work for you.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.