How can I map URLs to filenames with perl?
        Posted  
        
            by eugene y
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by eugene y
        
        
        
        Published on 2010-01-31T23:22:37Z
        Indexed on 
            2010/03/07
            23:25 UTC
        
        
        Read the original article
        Hit count: 199
        
In a simple webapp I need to map URLs to filenames or filepaths.
This app has a requirement that it can depend only on modules in the core Perl ditribution (5.6.0 and later). The problem is that filename length on most filesystems is limited to 255. Another limit is about 32k subdirectories in a single folder.
My solution:
my $filename = $url;
if (length($filename) > $MAXPATHLEN) { # if filename longer than 255
    my $part1 = substr($filename, 0, $MAXPATHLEN - 13);        # first 242 chars
    my $part2 = crypt(0, substr($filename, $MAXPATHLEN - 13)); # 13 chars hash
    $filename = $part1.$part2;
}
$filename =~ s!/!_!g; # escape directory separator
Is it reliable ? How can it be improved ?
© Stack Overflow or respective owner