On Aug 30, 2012, at 6:46 AM, Chris Stinemetz wrote: > Hello List, > > I am creating a program, where for the first time, I will be reading > in data from a socket port. > > I am a bit confused about how to print the processed data while still > reading in data from the port. Thus far, I have only processed data > from a file where the while loop ends when EOF criteria has been met. > > So my question is how do I continue to read in data from the port and > print the generated array as it is being created?
Print each record as it arrives, before adding it to the array. You might want to turn on auto flush for standard output: $|++; see 'perldoc -q flush' "How do I flush/unbuffer an output filehandle? Why must I do this?" > > snippet: > > #!/usr/bin/perl > > use strict; > use warnings; > use IO::Socket; > use Data::Dumper; > > > my $host = 'ip..address'; > my $port = portnum; > my $sock = new IO::Socket::INET( > PeerAddr => $host, > PeerPort => $port, > Proto => 'tcp' > ); > > die "cannot open socket $!" unless ($sock); > > my @array; > while ( my $line = <$sock> ) { print $line; or: > > do stuff and create array… print … stuff … > > push @array, [ .... stuff .... ]; > > } > ## print @array > for my $item (@array) { > print join(",",@$item),"\n"; > } Keep in mind that reading from a socket is not the same as reading from a file. Data records from a socket can be slow, late, incomplete, or arrive in fragments, depending upon how big your records are, how fast the socket server is generating records, how fast your client can read the records, and the speed of your network connection. Network communication can be unreliable, and your program must be able to handle the possible errors. See 'perldoc perlipc' for reading and writing sockets. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/