while($continue){ if ($socket->connected){ $quarter = $minute - ($minute % 15); ## if quarter != quarter 15 minutes has passed, create new filename with the next 0 15 30 45 designation if($cur_quarter != $quarter){ ## close current file and move it to directory for further processing close $fout; move("$dir.$filename","/home/ed/falbee/chrysler_asn/lu62asns"); $cur_quarter = $quarter; ## pad cur_quarter with 0 (make sure has two digits) $cur_quarter .= '0' x (2 - length($cur_quarter)); $filename ="$date$cur_quarter"; open ($fout, ">>", $filename); } local $/ = '^\'; my $line = <$socket>; print $fout $line; }