$text = "file1.txt"; $output = $text; while (-f $output){ $output .= ".new" } open(DAT,"$text") or die "Couldn't open $text for read: $!"; open(OUT,">$output") or die "Couldn't open $output for write: $!"; #your code goes here #stuff like while() #and print OUT $stuff_to_print;