OK, so if I understand correctly,

The first thing to notice is that the cat might be unnecesary. In other words,

cat file | gpg
should have the same effect as
gpg <file

but the second is simpler and eliminates an unnecesary pipeline stage. In either case the contents of file becomes the standard input for gpg. The only reason it would matter is if for some reason gpg really insisted that the passphrase-fd be a pipe.

If that will do, then you may be able to reduce the whole thing down to something as simple as this:

# Open the file open(FILE, "<file") || die; # Run gpg # Notice that we are not going to bother to try to # make the file become stdin to gpg. We're just going # to tell gpg which file descriptor it's already # accessible as. system("gpg", "--passphrase-fd=" . fileno(FILE), "--decrypt", "--output", "cleartext-filename", "ciphertext-filename"); close FILE;

That's the true intent of gpg's passphrase-fd option: to give it a file descriptor for some other file besides the three stdio ones.

You may notice that this can be improved a little bit because, in fact, the parent process (perl script) does not have to open the file at all, it only needs to be opened in the child process (the one that execs gpg). You can easily make that change to the above example but you have to play with fork, exec and waitpid in order to do so, and it does not seem worthwhile to me to do so since it does no harm to open the file in the parent process.


In reply to Re^3: using a pipe by Celada
in thread using a pipe by drock

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.