arkamedis21 has asked for the wisdom of the Perl Monks concerning the following question:

I have a C++ program that encodes a username and password into the registry. I am trying to built a perl/TK GUI as a front end to it. The C++ password program use getch(), which takes the characters off the keyboard without echoing it. I wanted to know if there is some kind of Perl way to send the password that I collect from my TK GUI to the pasword program, which needs some kind of keyboard input. Is there a perl way to redirect stdin to look like it came from the keyborad.

Any help would be greatly appreciated. Thanks.

Replies are listed 'Best First'.
Re: redirecting input to console
by waswas-fng (Curate) on Aug 22, 2002 at 15:07 UTC
    If you are using ActivePerl (Sounds like it) try getting the Expect module from PPM, else take a look at Expect. It lets you control input to other programs.

    -Waswas
Re: redirecting input to console
by Tanalis (Curate) on Aug 22, 2002 at 15:04 UTC
    From what I remember of getch, it takes input from a character buffer, defaulting to STDIN if that buffer is empty. Hence, redirecting something as input from a 'system' call should (hopefully) work:

    system("cpp_program < $password");

    Of course, I could be wrong about getch() and a character buffer ..

    Hope this is of some help.
    --Foxcub

      I dont think so, If I remember right the passwd program uses getch on most flavors of unix and it will not take input passed though STDIN for the password prompts..

      -Waswas
Re: redirecting input to console
by zentara (Cardinal) on Aug 22, 2002 at 16:39 UTC
     I tried to do something similar awhile back. I came to
    conclusion to use "named pipes" or better yet sockets to
    communicate between c and perl. I have a c example below,
    just rewrite your cpp program to read the data from the pipe,
    instead of STDIN. There are some tricks involved getting
    it all to run smoothly, but this gives you the idea.
    #include <stdio.h> int main(void){ FILE *fp; char *line; if (mkfifo ("my-pipe", 0755) != 0) {printf ("Could not make this fifo or it exists\n");} else {printf ("FIFO was successfully made!\n");} fp = fopen ("my-pipe", "r"); while (fgets (line,256, fp)){ printf ("message: %s", line); } close (fp); }
     Now your perl script should just write to the pipe
    #!/usr/bin/perl &send_data; sub send_data{ $|=1; open(FIFO, "> my-pipe") or die $!; print "What's your age?\n"; $input = <STDIN>; chomp $input; print FIFO "$input\n"; close(FIFO); }