Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, It seems I have lost a variable. I'm writing a CGI that will upload image files provided through an HTML form, then resize them and place them in the appropriate files.
#!C:\Perl\bin\perl.exe -wT ## use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; use File::Basename; $CGI::POST_MAX = 1024*5000; $CGI::DISABLE_UPLOADS = 0; my $query = CGI->new; my $safeCharacters = 'a-zA-Z0-9_.-'; my $uploadDirectory = 'C:/Apache/htdocs/notoriousteaze.com/Images/Temp +'; my $originalImage = $query->param("Image"); my ($filename, undef, $ext) = fileparse($originalImage, qr({\..*})); $filename .= $ext; $filename =~ tr/ /_/; $filename =~ s/[^$safeCharacters]//g; if ($filename =~ /^([$safeCharacters]+)$/) { $filename = $1; } else { error("That file name doesnt work for me. $filename"); } my $uploadFilename = $query->upload("Image"); open(UPLOADFILE, ">$uploadDirectory/$filename") or error('Could Not Up +load File.'); while ( <$uploadFilename> ) { print UPLOADFILE; } close UPLOADFILE; my %resizeFileType = ( ".jpeg" => \&resizeJpeg, ".jpg" => \&resizeJpeg, ".gif" => \&resizeGif, ".png" => \&resizePng, ); if(defined ($resizeFileType{$ext}) { $resizeFileType{$ext}->(); } else { error("Not seeing the filetype: $ext"); }
I'm not posting the subroutines because I never manage to reach them. I get stopped with "Not seeing the filetype: ", but $ext seems to be empty by the time I get there. I tried messing around with the scope of $ext (removing "use strict;" and "my"), to no avail. And yes, yes: I realize that I'm running with this on a Windows machine. Any help would be most appreciated. Thank you, kind monks. Cheers, K.

Replies are listed 'Best First'.
Re: A Wayward Variable
by apl (Monsignor) on Mar 26, 2008 at 17:16 UTC
    The statement
    if(defined ($resizeFileType{$ext})

    should have an additional ')' at the end. Otherwise, when ran, this statement produces syntax error at (...) line 53, near "}"

      Thanks for the catch!
Re: A Wayward Variable
by Pancho (Pilgrim) on Mar 26, 2008 at 17:12 UTC

    Just remove the curlies in

    my ($filename, undef, $ext) = fileparse($originalImage, qr({\..*}));

    this will give .ext for extension

    So to clarify... I think your issue is with the pattern you are using to match what will populate $ext

    Pancho
      Kalchas here, the Anonymous Monk who posted this post, now logged in. Removing the braces did the trick: thanks for your help, Pancho! Also, I benefited from the catch on the missing close paren on the if statement. Thanks for the super-quick help!
Re: A Wayward Variable
by Old_Gray_Bear (Bishop) on Mar 26, 2008 at 17:15 UTC
    Can you tell me what happens if you put a print(">>$originalImage<<\n"); after the line my $originalImage = $query->param("Image");?

    What should you do if param("Image") equals, say 'My-Picture' instead of 'My-Picture.jpg'?

    ----
    I Go Back to Sleep, Now.

    OGB

      Thanks for the reply. I got things sorted out by removing the braces. The point you raise is a good one, though. I'll be sure to review and make sure my users are inputting only good file types.