CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
subroutine woes



Feb 11, 2001, 7:22 AM

Post #1 of 2 (301 views)
subroutine woes Can't Post

i have a problem getting a simple subrotine to run, the code goes like this:

sub Upload_Pic {
use CGI qw/:standard/;
my $length;
if (!$_[0]) {
print "No file uploaded.";
print h2('File name'),@_;
open (TEST,"<$_[0]");
print h2('File MIME type'), uploadInfo($_[0])->{'Content-Type'};
while (<TEST>) {
#$length += length($_);
print h2('File length'),$length;
binmode FILE;
foreach $item(@test){
print FILE $item;

print end_html;

the routine on it's own worked fine and uploaded files with no fuss, but when i try to integrate it into another program, it' crashes.....
i have descovered that the problem lies with the command "uploadInfo($_[0])->{'Content-Type'}" but i am not sure why, and whenever i remove that command the file doesn't upload properly.......
could somebody PLEASE help, i will be eternally thankfull :)

Administrator / Moderator

Feb 11, 2001, 11:42 PM

Post #2 of 2 (291 views)
Re: subroutine woes [In reply to] Can't Post

First, a couple of comments:

1) The use statements can reside at the top of your program. Putting them in a sub doesn't mean they'll be loaded only when the sub is invoked. Modules included via the use is loaded at compile time, not runtime. So wherever a use statement may reside, it's still be fully loaded at compile time, whether or not the sub is ever actually invoked. May as well toss it at the top of your prog for clarity.

2) Where is @_ defined so you can use $_[0]? You're not passing anything to the sub, yet you're using my variables. If you're going to encapsulate, do it all the way :)

3) Assuming @_ is from user input, and $_[0] is the first element of the raw user input, never, ever, ever, use an untainted variable to open a file. open (TEST,"<$user_input_variable"); is one of the most common security risks. Imagine $user_input_variable being "../../../../../etc/passwd This may not apply in your code, but I don't see where @_ is derived from.

Now for the possible problem:

1) If it's working properly by itself, and it doesn't work when integrating it with other programs, it may be a variable locality issue. Check your other program to see if any variable names may have the same name as that which you are using in your sub. Better yet, use all my variables in the sub .. it doesn't look like any need to be globals anyway.

2) You're opening TEST for reading, and FILE for writing. The reasoning behind this is not understandable without commenting.

3) I don't see the actual upload code, but the uploadInfo($_[0])->{'Content-Type'}" line simply looks wrong. Using, it should look like this (assuming your new statement defined $in):

my $file = $in->uploadInfo($file_that_was_uploaded); 
my $mime_type = $file->{Content-Type'};

It's excellent that you're using Lincoln Stein's module, but it looks like it may be being used incorrectly. You might want to post (or better yet, attach) your code so we can see the whole code and offer more concise assistance.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives