@alltypes = qw(.gif .jpg .jpeg .jpe .jfif); for (1..5) { # No need for a c-style loop. if param("im$_") { ($filename = $ext = $file = param("im$_")) =~ s/.*[\/\\]//; # Simultaneous copy/substitute. $ext =~ s/^[^.]*(\.[\w]+$)/$1/; # There are better ways to check for a file, like grepping to make sure the file *ends* in the right extension, and getting rid of $ext, IMHO, unless you're using the extension somewhere...? unless (grep {$ext eq $_} @alltypes) { error("Your image needs to be a GIF of JPEG file!"); } } }