in reply to Re^3: Renaming an image file
in thread Renaming an image file

This line is confusing to me,
my $md5 = md5_hex( do{ local( @ARGV, $/) = $file; <> } );
If I run your code:
my $file = 'joe_34455667.jpg'; my $md5 = md5_hex( do{ local( @ARGV, $/) = $file; <> } ); $file =~ s[_(\d+)\.jpg$][_$md5.jpg];
I get "use of uninitialized value $_ in substitution (s///) My goal is just replace the file names with some name that is not going to be the same insisde of this directory, why replacing the number with the md5 of that number is a poor idea?

Replies are listed 'Best First'.
Re^5: Renaming an image file
by BrowserUk (Patriarch) on Nov 28, 2010 at 17:37 UTC
    This line is confusing to me, my $md5 = md5_hex( do{ local( @ARGV, $/) = $file; <> } );

    This bit: do{ local( @ARGV, $/) = $file; <> } reads the contents of the file whose name is $file.

    By all means spread that out across multiple lines if you prefer; Or perhaps use File::Slurp.

    I get "use of uninitialized value $_ in substitution (s///)

    It is very hard to see how that could happen, given that $_ isn't referenced anywhere in the line that has the substitution operator?

    why replacing the number with the md5 of that number is a poor idea?

    Let's say you have file joe_1.jpg and you rename it to joe_c4ca4238a0b923820dcc509a6f75849b,jpg, then next week joe uploads a new photo and calls it joe_1.jpg. Different photo, but same name. Overwrite.

    People tend to be very lazy and will likely stick to numbers if the low thousands, the potential for clashes is huge.

    But even if all the images are tiny, say just 10k bytes, that means there are 2.3836626600959079364564032010142e+24660 possibilities.

    Even with MD5 reducing that to 3.4028236692093846346337460743177e+38 the chances of collision are minuscule.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      At this point I already read the file in, is this looks ok based in what you have here?
      for(my $i=0;$i<4;$i++) { my $file = $q->param('pic_'.$i); my @full = split(/(\\)|(\/)/,$file); my $filename = pop(@full); $filename =~ s/\n//; $filename =~ s/\r//; $filename = $username_pic."_".$filename; #$filename=~s/^([^_]+_)(.*)(\.\w{3})\z/$1.md5_hex($2).$3/eg; my $md5 = md5_hex( $filename ); $filename =s[_(\d+).jpg$][_$md5.jpg]; }

        Sorry, I don't know much about cgi, but it still looks to me that you are md5'ing the filename and not the contents of the file.

        This is wrong: $filename =s[_(\d+).jpg$][_$md5.jpg];

        You mean =~, not =. That fixes the error mentioning $_ (s/// works on $_ by default).

        Also, the Perlish way to loop over an interval like that is for my $i (0..3) {.