I know the missing SWITCH is a style question where only the saintly or suicidal dare to prescribe - and i am not at all the former - but i must say that's some pretty baroque flow control you've got there.
so much so that i found it rather hard to answer your question. So here, instead of the answer you wanted, is something completely different. I've yanked it out of something i use daily, but chopped it around a lot, so it's not exactly tested. Comments follow.
#!/usr/bin/perl
use strict;
use Image::Size;
use CGI;
my $query = new CGI;
my %upload_fields = (
AUT_File => {
width => 640,
height => 480,
directory => 'Gallery/640x480',
},
THM_File => {
width => 80,
height => 60,
directory => 'Gallery/80x60',
},
);
for (keys %upload_fields) {
my $url = receive_upload( $query->upload($_), $upload_fields{$_} )
+ if $query->param($_);
# print confirmation?
}
# and carry on to handle rest of form.
# meanwhile:
sub receive_upload {
my ($filehandle, $parameters) = @_;
my $width = $parameters->{width};
my $height = $parameters->{height};
my $directory = $parameters->{directory};
my ($filewidth, $fileheight, $filetype) = imgsize( $filehandle );
throw_error("that's not an image file at all") unless $filewidth;
throw_error("that's not our kind of image file") unless $filetype
+=~ /jpeg/i || $filetype=~/png/i || $filetype=~/gif/i;
throw_error("that's not the right size: we insist on $width wide b
+y $height high")if $filewidth != $width || $fileheight != $height;
open (OUTFILE,">$directory/$filehandle") || die ("couldn't save '$
+directory/$filehandle'; $!");
my $bytesread = 0;
while ($bytesread = read($filehandle, my $buffer,1024)) { print OU
+TFILE $buffer }
close OUTFILE || die ("couldn't close newly uploaded file '$direct
+ory/$filehandle'; $!");
return $directory/$filehandle;
}
sub throw_error {
my $message = shift;
# do something sensible with $message
}
There are only two main differences between this and your version: it checks that the uploaded file has the right attributes, and the upload routines are abstracted out: if you want to change or add to the set of files that are received, you just have to change the %upload_fields hash.
Splitting the upload routines out into a sub like this will also make life easier later. When you come to manipulate the images rather than refusing bad ones, or want to do something cleverer to deal with duplicate filenames, you'll only have to make changes in one clearly identified place.
i partly wrote it out this way because it illustrates a few of perl's strengths, acquaintance with which might make your life easier:
- use strict will trap many common typos and other errors, among its many virtues
- complex data structures -
like the hash-of-hashrefs in $upload_fields - give you an economical way of passing round packages of related information, and in this case allow you to use named parameters for everything, making the code much easier to write and debug.
- compact, natural syntax - eg throw_error($foo) unless $bar; again, it lets you write quickly and read easily.
- special variables, eg $!: perl has lots of these powerful but enigmatic glyphs. This one comes out as the error message that was returned when the file operation you just attempted didn't work.
- CPAN - Image::Size is doing a lot of work for you here, and its authors are keeping an eye on a lot of arcane mailing lists that you therefore don't need to follow. There are hundreds more where that came from.
- tmtowtdi...
ps. i probably don't want to know what kind of images are being uploaded here, do i?
update. bit redundant, as it turns out, but hey. added some links anyway.
|