| What does this function do? I don't know perl |
majjk

msg:3959186 | 8:08 pm on Jul 24, 2009 (gmt 0) | Hi, I'm a php guy, and don't know much about perl... so could maybe someone help me out by explaining what this function does, step by step. I want to write a function in php that does the same thing... i.e. rename files that have been uploaded when the filename is not unique i the upload directory. sub rename_filename{ my $file_name = shift; my $count = shift; my $upload_dir = shift; my $path_to_file = $upload_dir . $file_name;
if(-e $path_to_file && -f $path_to_file){ if($file_name =~ /(.*)_(\d*)\.(.*)/){ # Already renamed so count on $count = $2 + 1; $file_name =~ s/(.*)_(\d*)\.(.*)/$1_$count\.$3/; } else{ # Not renamed so start counting $file_name =~ s/(.*)\.(.*)/$1_$count\.$2/; } &rename_filename($file_name, $count, $upload_dir); } else{ return $file_name; } }
Many thanks in advance!
|
jdMorgan

msg:3959212 | 9:06 pm on Jul 24, 2009 (gmt 0) | sub rename_filename{ # Shift filename from (default) array '@argv' to locally-scoped variable $file_name my $file_name = shift; # Shift count from array '@argv' to local variable $count my $count = shift; # Shift upload directory path from array '@argv' to local variable $upload_dir my $upload_dir = shift; # Build full path to file my $path_to_file = $upload_dir . $file_name;# If a file exists at that path and is an ordinary file if(-e $path_to_file && -f $path_to_file){ # then if filename is in the form "<anything-or-nothing>_<any-number-(including zero)-of-digits>.<anything-or-nothing>" if($file_name =~ /(.*)_(\d*)\.(.*)/){ # Filename was already used in renaming so increment the count $count = $2 + 1; # Replace previously-used filaname with initial name part, underscore, incremented count, dot, and filetype. $file_name =~ s/(.*)_(\d*)\.(.*)/$1_$count\.$3/; } else{ # else filename not already used, so just stick an underscore and the count in the middle $file_name =~ s/(.*)\.(.*)/$1_$count\.$2/; } # Rename the file &rename_filename($file_name, $count, $upload_dir); } # else file does not exist or is not an ordinary file else{ return $file_name; } }
I would strongly recommend replacing the regex pattern with something more specific, both for security and for performance reasons: Something like /^((\w+)+)_(\d+)\.([a-z0-9]+)$/ would be safer and faster to process. Note that the extra 'layer' of parentheses used around the first sub-pattern will require that back-references $2 and $3 above be changed to $3 and $4. Jim
|
majjk

msg:3959436 | 8:38 am on Jul 25, 2009 (gmt 0) | thanks a lot!
|
rocknbil

msg:3959505 | 2:52 pm on Jul 25, 2009 (gmt 0) | Also, this is redundant: if(-e $path_to_file && -f $path_to_file){ -e is "exists" and -f is "is file." So if it's a file, it exists. :-) With just -e, the resource may be a directory, alias, etc. if(-f $path_to_file){
|
perl_diver

msg:3959664 | 2:31 am on Jul 26, 2009 (gmt 0) | | # Shift filename from (default) array '@argv' to locally-scoped variable $file_name |
| inside a subroutine the array is @_ not @ARGV.
|
|
|