0

I have an existing perl script (which i cannot modify) which has two STDIN calls. I would like to run this perl script from a powershell script and feed it the two "user inputs" but using variables.

For a single input I tried

'input' | perl <file_path>.pl

This correctly uses the 'input' as the first value of the STDIN call. The issue is it only works once.

How can I pass two sets of STDIN inputs via the powershell script?

tommy
  • 11
  • 3
  • 1
    Does this not work?: `'input1', 'input2' | perl .pl` – Darin May 19 '22 at 21:13
  • There is no such thing as a "chomp call", in the way you are trying to use the word. The function `chomp` removes a trailing newline from a variable. I assume what you mean is that your Perl code expects two inputs, separated by newlines. As if a user entered two inputs manually. What is the Perl code that reads the inputs? – TLP May 19 '22 at 21:34
  • Why not try `perl .pl "$arg1" "$arg2"` where $arg1 and $arg2 are you shell script variables. – GoinOff May 19 '22 at 21:38
  • If this shell can use interpolated newlines, you could do `"input\ninput\n" | perl.pl` – TLP May 19 '22 at 22:02
  • my reference to chomp should have been . The perl script reads chomp($variablename = ); – tommy May 19 '22 at 22:11
  • The perl script input is not a two line input it is two separate STDIN calls. – tommy May 20 '22 at 16:56
  • Each use of `` in scalar context reads a line from STDIN. So if you use `` twice, you're reading two lines from STDIN (individually). – ikegami May 20 '22 at 19:01

1 Answers1

1

You are asking how to provide two lines to the STDIN of a program in PowerShell.

You can use either of the following:

"line1", "line2" | perl ...
"line1`nline2`n" | perl ...

Demo:

chomp( my $x = <STDIN> );
chomp( my $y = <STDIN> );
print( "<x:$x>\n" );
print( "<y:$y>\n" );
> "line1", "line2" | perl a.pl
<x:line1>
<y:line2>
> "line1`nline2`n" | perl a.pl
<x:line1>
<y:line2>

Note that a BOM gets added by PowerShell 5. No idea how to prevent that. This is not the case with newer versions of PowerShell.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Neither of these options work. The perl script is looking for two separate STDINs. These options will execute the first but pass nothing to the second. "line1", "line2" | perl ... "line1`nline2`n" | perl ... – tommy May 20 '22 at 16:53
  • There's no such thing as "two separate STDINs". That phrase makes no sense. If this doesn't answer the question, then start by fixing the question so we don't have to guess what you mean. – ikegami May 20 '22 at 17:18
  • 1
    There is an existing perl script that prompts the user for input via STDIN. It then processes that input. The script then reprompts the user for a second STDIN and processes that input. This is two separate and unrelated STDIN, no a two line single input. I am trying to call this existing perl script from powershell and feed it the two separate STDIN promptings without needing to type them in the command line. – tommy May 20 '22 at 17:42
  • Please stop repeating the same meaningless phrase. If you mean something other than reading two lines from STDIN, **fix your question** to clarify what you mean. – ikegami May 20 '22 at 19:07