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

Hi

I have a perl script from within which I need to call an .exe and pass a param and grab the return value. The problem is that the .exe issues a prompt and from with a shell script I would normally use a here document to pass the parameter

#!/bin/ksh myexe <<-EOF param1 EOF
This produces a reference number. e.g. 07F1E07F0007F0C

I don't have the source for myexe (a C program) but am required to use it's output. However from within the perl script I am not sure how to achieve the same thing. The current relevant perl code looks like this

my %NameAlreadyProcessed; for my $i (0 ..$#{$Details}) { my $Name = $Details->[$i][0]; my $contact = $Details->[$i][1]; next if $NameAlreadyProcessed{$Name}; $NameAlreadyProcessed{$Name} = 1; my $result = qx{ myexe $contact }; print "$Name $result\n"; }
This doesn't work . It's producing garbage in $result so I am assuming it's not being passed to myexe properly. Any help on this would be most welcome. I'm not a prolific perl programmer.

Replies are listed 'Best First'.
Re: passing params to an exe in a perl script
by Anonymous Monk on Jul 05, 2012 at 11:18 UTC
    Are you sure the bash syntax  myexe <<... isn't the same as pipe? same as  echo ... | myexe?
      If I use a pipe then I get the prompt as well as the result as part of the return value. Using a here document doesn't return the prompt.

        What qx is doing is calling the shell perl -V:sh, and shell usually doesn't like newlines (which is apparently why in bash you use heredoc), so you have to quote/escape/encode/serialize those args, for which you can use String::ShellQuote ( or Win32::ShellQuote )

        Or use IPC::Run3, its not backticks but it handles quoting for you

        Or use IPC::System::Simple capturex, its close to backticks, and it can handles quoting for you

        You could, of course, accept "the prompt as well as the result as part of the return value" and then remove the prompt -- with the likes of =~ s///;, among other possibilities.