Ahbeyra asked me about this idea. TMTOWTDI -- a hard way and an easy way.
The hard way:
Now, I should mention I haven't actually tried this in Perl, but it works in C and the Camel documents it...
The 'close on exec' flag is available through the Fcntl module as 'F_SETFD'. This flag is, at least in Perl, on by default execpt for 0, 1, and 2 (STDIN, STDOUT, and STDERR).
What you do is clear this flag on all the descriptors you need to keep open, do your exec, pass somehow on the command line which descriptors should be open , then start playing with your still-open files.
Then there's the easy way...
The Camel also mentions the $^F variable ( AKA $SYSTEM_FD_MAX ). According to the Camel, "When Perl opens a new file (or pipe or socket), it checks the current setting of the $^F ($SYSTEM_FD_MAX) variable. If the numeric file descriptor used by that new filehandle is greater than $^F, the descriptor is marked as one to close. Otherwise, Perl leaves it alone, and new programs you exec will inherit access."
So you could find your highest opened descriptor (or just choose an arbitrary very large number) and set $^F. Then, you can open your files, keep track of which are open in a temp file or on the command line for your new instance, and mess with your files via the descriptors in your newly exec()'ed process.
I hope this clears things up a bit. I think this is functionality that is too often overlooked. Also, I'm not sure if this is available everywhere Perl runs, as it seems to me that it may depend on at least a loosely POSIX-like environment. The Fcntl method does require fcntl() availability on your platform. I have never seen any documentation hinting that the $^F method is similarly limited. Some other monks I'm sure know the answer to that.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.