develooper Front page | perl.beginners | Postings from December 2002

IO::Handle, fdopen, and file descriptor #

December 31, 2002 11:30
IO::Handle, fdopen, and file descriptor #
Message ID:
I am making a call to gpg on the command line, a couple of the parameters that gpg will accept are file descriptor numbers that it then writes to, and I would like to capture that output and then read from it.

I have successfully made it read directly from a file on the local file system like so:

my $pass_fh = IO::File->new('/path/to/localfile');
fcntl $pass_fh, F_SETFD, 0; # don't close over exec
my $pass_fd = $pass_fh->fileno(); # get the file descriptor number

and then calling gpg on the command line like so:

gpg [other options here, etc.] --passphrase-fd $pass_fd [etc.]

Which works very well. And I can successfully create a new file handle,

my $logger_fh = IO::Handle->new();

And then presumably could use the same methods as above, but the file handle has to be open to determine its number and then to pass the file descriptor to the command line. Which I determined is what fdopen is for, so now to my question(s) (finally, sorry about that) how do I determine what the FD (file descriptor number) should be that I pass to fdopen? Is this an arbitrary number (not 0,1,2), can I have the system suggest a number?  If the program were to choose the same number in different forks would it matter?  Is there a better more appropriate way of handling all of this?  (Besides using one of the GPG modules from CPAN, which I would absolutely love, but after spending a number of days checking them out, each one has one or more quirks which doesn't allow it to work in our app.)

Thanks for your help and happy new year! Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About