in reply to Write to different file name
What this does is check for the existance of the file before it tries to open it for write (which truncates the file, btw). If the file exists, it appends a ".new" to the end of the file name, and checks for existance again. When it succeeds, you know that you've got a filename that you can safely open without truncating. Of course, this method does have its flaws. For instance, what if you have two versions of the script running on the same file at the same time? Oh well, you've got something to go on at least...;)$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(<DAT>) #and print OUT $stuff_to_print;
thor
|
|---|