#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Fcntl qw(:flock); # import LOCK_* and SEEK_END constants use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END); #SEEK_SET=0 SEEK_CUR=1 ... my @doc_write; my @array; my $file = "test.txt"; sub add { open (DATA, "+<", $file) or die ("Could not open file: ".$file.". $!\n"); flock(DATA, LOCK_EX) or die "Could not lock '".$file."' - $!\n"; if (-z "".$file."") { print DATA "0\n"; } while( @doc_write = ) { chomp @doc_write; seek(DATA , 0 , SEEK_SET) or die "Cannot seek - $!\n"; truncate(DATA, 0); my $range = 50; my $minimum = 100; my $random_number = int(rand($range)) + $minimum; my $time = time(); my $packet = join (' ' , $time , $random_number); push(@doc_write , $packet); print Dumper (\@doc_write); foreach $_ (@doc_write) { print DATA $_ . "\n"; } close (DATA) or die "Could not close '".$file."' - $!\n"; return $packet; } # End of While () } # End of sub add while (sleep 1) { my $losses = &add(); print "Added:" .$losses. "\n"; } #### while (sleep 1) { read all data into an array... extract last element... process it.. write to a secondary file the extracted data for comparison purposes.. }