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:

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.


In reply to Re: combined text&files by thpfft
in thread combined text&files by PriNet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.