Search code examples
perlio

Perl file descriptor streaming


I am trying to modify this Perl code to do what I need. Right now it reads data from a file descriptor and once 8000 bytes are buffered it completes and moved on to write them into a file.

I am trying to get it to continue to read every 8000 bytes from that stream and only stop when the script is killed or X numbers of cycles go by. So it would keep adding 8000 bytes every time they are buffered or just write data to the file every X number of seconds.

I am not familiar with streams and have not written in perl, cant find any documentation that i could apply to this, although I think the solution is faily straigh forward. I will appreciate the help, will post any new updates

 use warnings;
 use strict;

 use IO::Handle;

 $| = 1; 
 my $buffer = undef;
 my $result = undef;
 my $AUDIO_FD = 3;   
 my $audio_fh = new IO::Handle;
 $audio_fh->fdopen( $AUDIO_FD, "r" );         


 my $bytes_read = $audio_fh->read( $buffer, 8000 );
 $audio_fh->close();


 my $fh;
 open( $fh, ">/tmp/rawdata.txt" );
 print $fh $buffer;
 close( $fh );

Solution

  • This approach will not allow reading from file continuously, however, I was able to get constant data stream from same file descriptor using bash cp command