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

Dear Monks,
The following is a cgi designed to upload a file and search and pull out individual case-id's from the rest of the mess. The file uploads are generally plaintext and html. Some get to be rather large, 5 megs or so. I have it working right now, but It will only return 1 case-id from a list of fifty. I suspect that the problem is in my choice of the $cases variable. However I'm not sure, just stuck in the mental mud.
Any one know how to retrieve all of the case-id's?
#!/usr/bin/perl -w ### Version ### ### 1.1.1 Working on file upload and loop ### 1.1.2 File will upload but pulls only 1 id ### muppet.cgi ### file based case id grabber ### use strict; use CGI qw/:standard/; ### Print HTML ### print header, start_html('The Muppet'), h1('Waka_W4k4!'); print_form() unless param; print_results() if param; print end_html; ### Sub Print Form ### ### Form Subset ### sub print_form { print start_multipart_form(), filefield(-name=>'payload',-size=>60),br, submit(-label=>'Upload File'), end_form; } ### Sub Print Results ### ### Do work and print results ### sub print_results { ### Payload Variables ### my $file = param('payload'); $file=~m/^.*(\\|\/)(.*)/; my $file_name = $2; my $upload_file = upload('payload'); my $upload_dir = "/var/www/html/abuse"; ### Check for File Upload ### if (!$file) { print "No file uploaded."; return; } ### Write Uploaded file to disk ### open UPLOADFILE, ">$upload_dir/$file_name"; while (<$upload_file>) { print UPLOADFILE; } close UPLOADFILE; ### Prepare the goods to be delivered ### my $goods = "/var/www/html/abuse/$file_name"; my $caseid = 'Case - '; my $cases; open GOODS, "$goods"; my @goods=<GOODS>; close GOODS; foreach(@goods){ if (s/$caseid//){ $cases = $_; } } ### Print the Relavent Information ### print h2('File Name'),$file_name; print h2('File MIME type'), uploadInfo($file)->{'Content-Type' +}; print h2('Cases'), $cases; print h2('<A HREF="muppet.cgi">Try Again</A>'); } ### End of Sub Print Results ### ### FIN ###

peace,
amearse

Replies are listed 'Best First'.
Re: File upload and regex
by particle (Vicar) on Mar 02, 2002 at 00:19 UTC
    there are two ways to fix this.

    1> changing three lines of code to use an array

    # ...snip... my @cases; # make this an array # ...snip... foreach(@goods){ if (s/$caseid//){ push @cases, $_; # add caseid to array } } # ...snip... print h2('Cases'), "@cases"; # print the cases # ...snip...
    -or-

    2> changing one line of code to append to the string

    # ...snip... my $cases; # keep this as an empty string # ...snip... foreach(@goods){ if (s/$caseid//){ $cases .= $_ . ' '; # append caseid to string } } # ...snip... print h2('Cases'), $cases; # print the cases # ...snip...

    ~Particle ;Þ

      Big thanks to particle and grep. I just needed a little push.
Re: File upload and regex
by grep (Monsignor) on Mar 02, 2002 at 00:25 UTC
    I imagine you problem is here:
    foreach(@goods){ if (s/$caseid//){ $cases = $_; } }
    You are taking $_ and removing 'CASE -' when it matches and then you copy the remainder in to $cases. Then you rewrite $cases in each iteration. I believe you want:
    push(@cases,$_);

    Some things that would make it helpful to other monks debugging you problem:
  • check the success of your open's - I can not stress how important this is
  • indentation - you have several different styles? of indentation going on
  • commenting - thank you for commenting your code - but I would also recommend lining up you comments with your code and using whitespace between sections to denote logical sections




  • grep
    grep> grep clue /home/users/*