0

Why does the following perl script not print anything?

if (open(my $fh, "| (while read LINE; do echo \$LINE; done )")) {
    print $fh "test";
    close($fh);
}

In sh, if I run echo 'test' | (while read LINE; do echo \$LINE; done ) I would expect to have test be printed out.

2
  • Add "\n" to your print, like this print $fh "test\n"; Commented Aug 31, 2014 at 17:37
  • Side note, open my $fh, "|-", ".." as perl favors three args open. ("-|" to rad from pipe) Commented Aug 31, 2014 at 17:49

1 Answer 1

1

Add "\n" to your print, since read is looking for a whole line, like this

print $fh "test\n";
Sign up to request clarification or add additional context in comments.

3 Comments

It is best to $fh->autoflush so that you can print what you want
@Borodin, The thing is, test+newline is what he wants to print. $fh->autoflush won't help at all here.
@ikegami I have removed the erroneous part of the explanation - thank you for pointing it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.