| [reply] [d/l] |
I've a slightly different question but relevant in this context I guess:
If I want to open an errorlog file in the main calling program , how can I pass the filehandle as argument to each subsequent script call ?
Thanks
| [reply] |
Inheritance of file handles in child processes is very similar between Unix and Windows. All of the same concepts are there in both places, but they have different names and are manipulated using different functions. Perl layers the Unix-style concepts over the top of the Windows equivalents. If you want to have a Perl child process on Windows inherit a file handle, it requires you to use the Windows concepts and then rewrap them in the Unix equivalents so Perl can use them (because Perl only does that for you automatically for STDIN, STDOUT, and STDERR). Win32API::File (bundled with Perl) provides all of the tools you need to do this.
In Unix, to share a file handle with a child process that is not running the same script, that is, when you did fork(2) followed by exec (perhaps indirectly, such as via system), you have to:
- mark the file descriptor as "do not close on exec" (there are 2 ways to do that)
- fork the child and exec the new program in the child
- inform the child of the file descriptor (a small integer)
- have the child re-open the file descriptor (by passing something like "<&=$fd" to open)
In Windows, the steps are almost the same. Instead of a file descriptor, you deal with a native Windows "file handle" (a pointer, which is just a large integer), which you must not confuse with a Perl "file handle". The Windows steps are:
- mark the native handle so it will be inherited [using HANDLE_FLAG_INHERIT]
- spawn the child (don't use Perl's fork as that doesn't spawn a child, it creates a thread and a new interpreter instance)
- inform the child of the native handle, a large integer [requires GetOsFHandle()]
- have the child re-open a Perl file handle that just wraps the given native handle [using OsFHandleOpen()]
I've done this successfully without much trouble before. I thought I'd posted code for such here but my search did not turn such up. I might rewrite that later and post it.
See also: Re^2: Passing a File Descriptor to a New Process on Windows (Win32API::File)
| [reply] [d/l] |
I haven't tested this, but I believe that if you (re)open stderr to point to your logfile, and each of your subscripts simply write to stderr, then all the output will end up in the log file.
You could also pass the name of the file as an argument to each of the subscripts and have each of them open it individually; though -- on windows at least -- you would have to close it in your main script before calling the subscripts.
Actually passing file handles between separate processes doesn't work.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] |