You can use sysread to read any available bytes from the stream without worrying about whether there is a newline in there or not. Put whatever you read into a buffer in case it was only part of a transaction...
#!/usr/bin/perl -w
use strict; use warnings;
use IO::Socket;
use IO::Select;
my $listener = IO::Socket::INET->new(
LocalPort => 42424,
Listen => 1,
) or die "failed to create socket: $!";
my $select = IO::Select->new( $listener );
my %buffers = ();
while ( my @ready = $select->can_read ) {
foreach my $fh ( @ready ) {
if ( $fh == $listener ) {
my $new = $listener->accept;
$buffers{ $new } = '';
$select->add( $new );
} else {
$fh->sysread( $buffers{ $fh }, 1024, length( $buffers{ $fh
+ } ) );
while ( $buffers{ $fh } =~ s/STX(.*?)ETX// ) {
process( $1 );
}
}
}
}
sub process {
print "GOT: $_[0]\n";
}