My perl cgi script is working, however it is very ugly. I am hopeing that a guru could help to teach me how to write more elegant code.
The code below is a section of a script that reads and uploads a series of fields and files, for each of six products. The chunks of code to upload the data are almost exactly the same, except they differ in the filenames according to the product number (eg. the code below is for products 3 and 4).
I _think_ I need to get these into a conditional loop and work in an array of filenames or a counter. Any tips or code examples would be greatly appruciated.
Matt
#this section uploads 2 images (photo3_large,photo3) and several text and checkbox fields for product 3.
$filename3l = $query->param("photo3_large"
$filename3_text = $query->param("photo3_text"
$filename3_name = $query->param("photo3_name"
$filename3_price = $query->param("photo3_price"
$filename3_replace = $query->param("photo3_replace"
$filename3l =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle3l = $query->upload("photo3_large"
open UPLOADFILE3l, ">$upload_dir/$filename3l";
while ( <$upload_filehandle3l> )
{
print UPLOADFILE3l;
}
close UPLOADFILE3l;
$filename3s = $query->param("photo3"
$filename3s =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle3s = $query->upload("photo3"
open UPLOADFILE3s, ">$upload_dir/$filename3s";
while ( <$upload_filehandle3s> )
{
print UPLOADFILE3s;
}
close UPLOADFILE3s;
#this section uploads 2 images (photo4_large,photo4) and several text and checkbox fields for product 4.
$filename4l = $query->param("photo4_large"
$filename4_text = $query->param("photo4_text"
$filename4_name = $query->param("photo4_name"
$filename4_price = $query->param("photo4_price"
$filename4_replace = $query->param("photo4_replace"
$filename4l =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle4l = $query->upload("photo4_large"
open UPLOADFILE4l, ">$upload_dir/$filename4l";
while ( <$upload_filehandle4l> )
{
print UPLOADFILE4l;
}
close UPLOADFILE4l;
$filename4s = $query->param("photo4"
$filename4s =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle4s = $query->upload("photo4"
open UPLOADFILE4s, ">$upload_dir/$filename4s";
while ( <$upload_filehandle4s> )
{
print UPLOADFILE4s;
}
close UPLOADFILE4s;
The code below is a section of a script that reads and uploads a series of fields and files, for each of six products. The chunks of code to upload the data are almost exactly the same, except they differ in the filenames according to the product number (eg. the code below is for products 3 and 4).
I _think_ I need to get these into a conditional loop and work in an array of filenames or a counter. Any tips or code examples would be greatly appruciated.
Matt
#this section uploads 2 images (photo3_large,photo3) and several text and checkbox fields for product 3.
$filename3l = $query->param("photo3_large"
$filename3_text = $query->param("photo3_text"
$filename3_name = $query->param("photo3_name"
$filename3_price = $query->param("photo3_price"
$filename3_replace = $query->param("photo3_replace"
$filename3l =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle3l = $query->upload("photo3_large"
open UPLOADFILE3l, ">$upload_dir/$filename3l";
while ( <$upload_filehandle3l> )
{
print UPLOADFILE3l;
}
close UPLOADFILE3l;
$filename3s = $query->param("photo3"
$filename3s =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle3s = $query->upload("photo3"
open UPLOADFILE3s, ">$upload_dir/$filename3s";
while ( <$upload_filehandle3s> )
{
print UPLOADFILE3s;
}
close UPLOADFILE3s;
#this section uploads 2 images (photo4_large,photo4) and several text and checkbox fields for product 4.
$filename4l = $query->param("photo4_large"
$filename4_text = $query->param("photo4_text"
$filename4_name = $query->param("photo4_name"
$filename4_price = $query->param("photo4_price"
$filename4_replace = $query->param("photo4_replace"
$filename4l =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle4l = $query->upload("photo4_large"
open UPLOADFILE4l, ">$upload_dir/$filename4l";
while ( <$upload_filehandle4l> )
{
print UPLOADFILE4l;
}
close UPLOADFILE4l;
$filename4s = $query->param("photo4"
$filename4s =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle4s = $query->upload("photo4"
open UPLOADFILE4s, ">$upload_dir/$filename4s";
while ( <$upload_filehandle4s> )
{
print UPLOADFILE4s;
}
close UPLOADFILE4s;