Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   Testing Filenames in Perl (http://www.go4expert.com/articles/testing-filenames-perl-t2555/)

pradeep 15Jan2007 16:08

Testing Filenames in Perl
 
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.

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/;
}

So, you'll get mixtures of pretty and ugly filenames, only occasionally.

oleber 24Apr2007 13:09

Re: Testing Filenames in Perl
 
Many times not logic names are necessary, MD5 can be used in here.

I'm using something like:

Code:

use strict;
use Digest::MD5 qw(md5_hex);

my $secretText = "xpto";
my $counter = 0;
my $file_name = "";
do {
        $file_name = md5_hex($$.$^T.$secretText.($counter++)).".html";
} while -e $file_name;

The $secretText helps to create a not logic identifier since it is personal.
The $counter forces different text every time.

pradeep 25Apr2007 10:14

Re: Testing Filenames in Perl
 
That's a really cool idea too, but most of the times we need the files to be named according to some logic!

oleber 26Apr2007 12:48

Re: Testing Filenames in Perl
 
Logic parts can be added to the name ;)

But the important part is the cycle to find a acceptable name. Probably (I don't work with the web) many web servers are using a process for multiple requests, so the PID will be the same for this requests. Adding a counter can help.

pradeep 26Apr2007 13:13

Re: Testing Filenames in Perl
 
Yes, quite right, web servers like Apache use threads to serve multiple requests. You code is perfect except for hashing.
I think, timestamp,PID and counter are enough to make the filename unique!

pradeep 26Apr2007 13:17

Re: Testing Filenames in Perl
 
We can make a sub-routine out of this logic, like this,

Code: Perl

sub getFreeFilename
 {
     my $counter = 0;
     my $file_name = "";
     do {
         $file_name = sprintf("%s%s%s.html",$$,$^T,$counter++);
     } while -e $file_name;
 
     return $file_name;
 }


oleber 26Apr2007 13:27

Re: Testing Filenames in Perl
 
Even better lets do $counter a global variable.

Quote:

{
my $counter = 0;
sub getFreeFilename {
while(1) {
my $file_name = sprintf("%s_%s_%s.html",$$,$^T,$counter++);
return $file_name if not -e $file_name;
};
}
}
Since all this code is inside { }, the $count is just visible inside { } and will exist while the subroutine is existing (+- as a static variable in C++). So no real loop is necessary.

oleber 26Apr2007 13:30

Re: Testing Filenames in Perl
 
sory for the code

Code:

{
  my $counter = 0;
  sub getFreeFilename {
    while(1) {
      my $file_name = sprintf("%s_%s_%s.html",$$,$^T,$counter++);
      return $file_name if not -e $file_name;
    };
  }
}



All times are GMT +5.5. The time now is 03:27.