For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2005 > Re: using open3 to interact with external program--ANSWER FOUND









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: using open3 to interact with external program--ANSWER FOUND
Zentara

2005-06-09, 8:56 pm

On Wed, 08 Jun 2005 09:24:43 +0100, darren@birkett.com (D. J. Birkett)
wrote:
[color=darkred]
>Thomas Bätzler wrote:

Well I'm a "zen hacker" and like a million monkeys, I sometimes get
lucky.

You may need the latest gpg 1.4.1 for this to work, but I may be wrong.
Anyways, there is a new option for sending commands to gpg, and you
can do what you want with the following script. Try it, it works.

First set the string to "trust\n1\ny\n" then to "trust\n\5\y\n".
Its funny how 'your' first attempt wasn't so bad after all.

The secret is the --no-tty option in conjunction with --command-fd
Search the gpg source distribution for details about these commands.

It certainly makes sense now that I see it.

#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;

local $SIG{CHLD} = 'IGNORE';
local $SIG{PIPE} = 'IGNORE';

my $childpid = open3(\*IN, \*OUT, \*ERR,
'gpg --no-greeting --no-tty --command-fd 0 --status-fd 1 --edit
zentara');

print IN "trust\n5\ny\n";
close IN;

my(@answer,@err) = ((),());

@answer = <OUT>;
print "out->@answer\n";

@err = <ERR>;
print "err->@err\n";

__END__






--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
John W. Krahn

2005-06-09, 8:56 pm

zentara wrote:
> On Wed, 08 Jun 2005 09:24:43 +0100, darren@birkett.com (D. J. Birkett)
> wrote:
>
>
> Well I'm a "zen hacker" and like a million monkeys, I sometimes get
> lucky.
>
> You may need the latest gpg 1.4.1 for this to work, but I may be wrong.
> Anyways, there is a new option for sending commands to gpg, and you
> can do what you want with the following script. Try it, it works.
>
> First set the string to "trust\n1\ny\n" then to "trust\n\5\y\n".
> Its funny how 'your' first attempt wasn't so bad after all.
>
> The secret is the --no-tty option in conjunction with --command-fd
> Search the gpg source distribution for details about these commands.
>
> It certainly makes sense now that I see it.
>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use IPC::Open3;
>
> local $SIG{CHLD} = 'IGNORE';
> local $SIG{PIPE} = 'IGNORE';
>
> my $childpid = open3(\*IN, \*OUT, \*ERR,
> 'gpg --no-greeting --no-tty --command-fd 0 --status-fd 1 --edit
> zentara');
>
> print IN "trust\n5\ny\n";
> close IN;
>
> my(@answer,@err) = ((),());


That makes no sense at all, and this being a beginners list I have to point it
out.

perldoc perldata
[snip]
You can actually put an array or hash anywhere in the list, but the
first one in the list will soak up all the values, and anything after
it will become undefined.


So the array @answer will receive everything from the assignment. But the
assignment is superfluous because my() creates empty arrays.


> @answer = <OUT>;
> print "out->@answer\n";
>
> @err = <ERR>;
> print "err->@err\n";


A "more correct" idiom is:

my @answer = <OUT>;
print "out->@answer\n";

my @err = <ERR>;
print "err->@err\n";

__END__



John
--
use Perl;
program
fulfillment
Zentara

2005-06-10, 8:55 am

On Thu, 09 Jun 2005 14:10:28 -0700, krahnj@telus.net (John W. Krahn)
wrote:

>
>That makes no sense at all, and this being a beginners list I have to point it
>out.


Yes thanks for pointing it out. I should have just
my(@answer,@err);


I appreciate your efforts to keep us correct. :-)




--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com