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

I've written a very basic chat program that transmits data across the net to another chat program running elsewhere. I'd like to modify a subroutine so that it accepts byte-sized input rather than line input from STDIN. Here was my last two attempts:
sub user_to_host { my $s = shift; # Note: $s is the socket my $buffer; STDIN->autoflush(1); while (read (STDIN, $buffer, 1)) { print $s $buffer; } } # ATTEMPT #2 sub user_to_host { my $s = shift; my $buffer; STDIN->autoflush(1); while ($s) { read (STDIN, $buffer, 1); print $s $buffer; } }
I'm still getting line input and output, however. The input is running in one process and the output is running in another. In other words, the output subroutine above is the only part of the program running so the problem is definitely within the bounds of this subroutine. I turn humbly to the PerlMonks community for wisdom.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Getting byte stream input from STDIN
by bikeNomad (Priest) on Jul 27, 2001 at 00:02 UTC
    You don't say what STDIN is connected to. If it's a terminal, you're suffering from the standard behavior of line editing. You may (depending on OS) be able to turn off cooked/editing mode. To do this, you could use something like Term::ReadKey that does this for you.

    As far as STDOUT appearing in line mode, you haven't turned off buffering there, have you? If you have, prints should appear byte by byte.

      Yes, forgot to mention that autoflushing on STDOUT is enabled as well. I'll look into Term::ReadKey...the pod for getc mentions this, too. Thanks.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

Re: Getting byte stream input from STDIN
by larryk (Friar) on Jul 27, 2001 at 01:03 UTC
    $/ = \1; while (<STDIN>) { print "The one byte that I just got is $_\n"; }
    I looked but I can't find the doc to back it up but it works - you just make the line terminator a reference to a number (whatever that's called - please enlighten me) and <FH> reads that many bytes from FH at a time. I think it's a bit quicker than getc as well.

    larryk

    "Argument is futile - you will be ignorralated!"

Re: Getting byte stream input from STDIN
by nysus (Parson) on Jul 27, 2001 at 00:03 UTC
    Addendum: I stumbled upon the getc function in my research and that doesn't seem to work at all. Could the fact that I'm on a Windows machine using command.com be part of the problem?

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      The docs for getc say that it won't get you past the problem of having to hit ENTER.