When your script writes to a new file, you probably want it to create a new and unique name for the new file, one that doesn't conflict with any existing files, which would be overwritten. One way to create a new file name that's unique is to incorporate the process id and the time into the name. Perl's special variable, $$ returns the current pid and $^T returns the timestamp (in seconds since 1970). So you could use something like $filename = "$$" . "$^T" . ".html"; This alone will not guarantee uniqueness since there are only a finite number of process ids, which are recycled, and your script could have been accessed twice within the same second.
This also results in ugly filenames, something like "213031168857899.html". If you have prettier names that you insist on, you can test for the existence of a file with the proposed new name, using Perl's -e operator. -e $file_name is true if a file already exists with that name. In the example below, the variable $text holds some key text taken from the contents of the file that we want to use in the name. We're also assuming that the script is writing a web page, so we add ".html" as the extension.
So, you'll get mixtures of pretty and ugly filenames, only occasionally.
This also results in ugly filenames, something like "213031168857899.html". If you have prettier names that you insist on, you can test for the existence of a file with the proposed new name, using Perl's -e operator. -e $file_name is true if a file already exists with that name. In the example below, the variable $text holds some key text taken from the contents of the file that we want to use in the name. We're also assuming that the script is writing a web page, so we add ".html" as the extension.
Code: Perl
$file_name = $your_chosen_dir . $text . ".html";
if ( -e $file_name )
{
## do something to make it different, like
## substitute pidtime.html for html at the end
$file_name =~ s/html$/$$^T\.html/;
}


