title: How to read to and write from a pipe in Perl?
body: <p>I need to modify an existing Perl program. I want to pipe a string (which can contain multiple lines) through an external program and read the output from this program. This external program is used to modify the string. Let's simply use <code>cat</code> as a filter program. I tried it like this, but it doesn't work. (Output of <code>cat</code> goes to STDOUT instead of being read by <code>perl</code>.)</p>
  <pre><code>#!/usr/bin/perl
  
  open(MESSAGE, &quot;| cat |&quot;) or die(&quot;cat failed\n&quot;);
  print MESSAGE &quot;Line 1\nLine 2\n&quot;;
  my $message = &quot;&quot;;
  while (&lt;MESSAGE&gt;)
  {
      $message .= $_;
  }
  close(MESSAGE);
  print &quot;This is the message: $message\n&quot;;
  </code></pre>
  <p>I've read that this isn't supported by Perl because it may end up in a deadlock, and I can understand it. But how do I do it then?</p>