Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:28:44+00:00 2026-06-12T00:28:44+00:00

I am trying to communicate with an interactive process. I want my perl script

  • 0

I am trying to communicate with an interactive process. I want my perl script to be a “moddle man” between the user and the process. The process puts text to stdout, prompts the user for a command, puts more text to stdout, prompts the user for a command, ……. A primitive graphic is provided:

 User <----STDOUT---- interface.pl <-----STDOUT--- Process
 User -----STDIN----> interface.pl ------STDIN---> Process
 User <----STDOUT---- interface.pl <-----STDOUT--- Process
 User -----STDIN----> interface.pl ------STDIN---> Process
 User <----STDOUT---- interface.pl <-----STDOUT--- Process
 User -----STDIN----> interface.pl ------STDIN---> Process

The following simulates what I’m trying to do:

    #!/usr/bin/perl

    use strict;
    use warnings;

    use FileHandle;
    use IPC::Open2;
    my  $pid = open2( \*READER, \*WRITER, "cat -n" );
    WRITER->autoflush(); # default here, actually
    my $got = "";
    my $input = " ";

    while ($input ne "") {
            chomp($input = <STDIN>);
            print WRITER "$input \n";
            $got = <READER>;
            print $got;
    }

DUe to output buffering the above example does not work. No matter what text is typed in, or how many enters are pressed the program just sits there. The way to fix it is to issue:

    my  $pid = open2( \*READER, \*WRITER, "cat -un" );

Notice “cat -un” as opposed to just “cat -n”. -u turns off output buffering on cat. When output buffering is turned off this works. The process I am trying to interact with most likely buffers output as I am facing the same issues with “cat -n”. Unfortunately I can not turn off output buffering on the process I am communicating with, so how do I handle this issue?

UPDATE1 (using ptty):

    #!/usr/bin/perl

    use strict;
    use warnings;

    use IO::Pty;
    use IPC::Open2;

    my $reader = new IO::Pty;
    my $writer = new IO::Pty;

    my  $pid = open2( $reader, $writer, "cat -n" );
    my $got = "";
    my $input = " ";

    $writer->autoflush(1);

    while ($input ne "") {
            chomp($input = <STDIN>);
            $writer->print("$input \n");
            $got = $reader->getline;
            print $got;
    }

~

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:28:46+00:00Added an answer on June 12, 2026 at 12:28 am

    There are three kinds of buffering:

    1. Block buffering: Output is placed into a fixed-sized buffer. The buffer is flushed when it becomes full. You’ll see the output come out in chunks.
    2. Line buffering: Output is placed into a fixed-sized buffer. The buffer is flushed when a newline is added to the buffer and when it becomes full.
    3. No buffering: Output is passed directly to the OS.

    In Perl, buffering works as follows:

    • File handles are buffered by default. One exception: STDERR is not buffered by default.
    • Block buffering is used. One exception: STDOUT is line buffered if and only if it’s connected to a terminal.
    • Reading from STDIN flushes the buffer for STDOUT.
    • Until recently, Perl used 4KB buffers. Now, the default is 8KB, but that can be changed when Perl is built.

    This first two are surprisingly standard across all applications. That means:

    • User -------> interface.pl

      User is a person. He doesn’t buffer per say, though it’s a very slow source of data. OK

    • interface.pl ----> Process

      interface.pl‘s output is block buffered. BAD

      Fixed by adding the following to interface.pl:

      use IO::Handle qw( );
      WRITER->autoflush(1);
      
    • Process ----> interface.pl

      Process’s output is block buffered. BAD

      Fixed by adding the following to Process:

      use IO::Handle qw( );
      STDOUT->autoflush(1);
      

      Now, you’re probably going to tell me you can’t change Process. If so, that leaves you three options:

      • Use a command line or configuration option provided by tool to change its buffering behaviour. I don’t know of any tools that provide such an option.
      • Fool the child to use line buffering instead of block buffering by using a pseudo tty instead of a pipe.
      • Quitting.

    • interface.pl -------> User

      interface.pl‘s output is line buffered. OK (right?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to communicate between a flex 4.1 application to a flash action script
When trying to communicate between my Content- and Background Script I get the following
I have been trying to communicate between two CodeIgniter projects. I have 2 projects,
I am trying to communicate between an applet and a servlet. I first tried
I am trying to communicate between two Windows applications in Delphi. Sender sends commands
I am currently trying to communicate a parent process which should have multiple children
I am trying to communicate between two android emulators for communication , thus I
I am trying to communicate between two processes. From MSDN Documentation, I came across
I'm trying to communicate between my PC (Windows 7 using Netbeans and RXTX) with
I'm trying to communicate between two computers through the TCP/IP protocol on C++. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.