use strict; use warnings; my $cli = 'file.txt'; open (FILE,"<$cli") or die $!; while (my $cli_str = ) {  nextline($cli_str); } closefh(); close FILE; {  my $fh;  sub nextline {    my (@cli_str) = @_;    print @cli_str;    unless ($fh && defined fileno $fh) {      open $fh, ">data.txt" or die $!;    }    print $fh @cli_str;  }  sub closefh {    close $fh;    undef $fh;  } }