At some point I wanted to use ECDSA signing capabilities of bitcoin address. But it's not easy, and the bitcoin client doesn't provide any tool for that. And I was convinced that it should not.
Using GnuPG would be nice, but I realised that it's a pain in the ass to program with. It realy doesn't seem to me that it is suitable for scripting.
I felt your pain when dealing with GnuPG scripting especially with how file handles are managed.
Until I discovered the Perl API to access GnuPG called GnuPG::Interface (made by Jesse Vincent):
http://search.cpan.org/dist/GnuPG-Interface/http://search.cpan.org/dist/GnuPG-Interface/lib/GnuPG/Interface.pmThis is working great and avoid the classical glitches.
Hope this helps,