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

*Update: I do not need it to be uploaded to a server, only temporarily interpreted, and the resulting variables stored in variables need to be printed on screen (dont know if that makes a difference)

Hello everyone, I am sorry if I am asking something that can be pieced together from the forums, but i have searched google and forums for a few hours now, and cant get this code to work. *all of these codes work fine by them selves, but assembling them seems to be a problem.

What i am trying to do is make a simple form, have an upload box for a file which contains data. This file is then processed via perl script, and then the results need to print out in the browser.

I have managed to create a basic form and know it is passing things to the perl script, but for some reason after that, i can print statements BEFORE ANY of the "processing", but after, none of the printing shows in the browser.

Also, all of the lines which print in the terminal, do not print in the html page. I am really lost and any help would be appreciated, below is the code for the 3 files.

Thank you for any assistance you can provide

concepts.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html lang="en"> <head> <title>Form Example</title> </head> <body> <!-- Insert your content here --> <form action="cgi-bin/concepts2.pl" method="post"> <p>File:<input type="FILE" name="file"> <input type="submit" name="submit" value="Submit"> </form> </body> </html>
concepts2.pl #!/usr/bin/perl use strict; use CGI; my $cgi=new CGI; print $cgi->header(); print $cgi->start_html(-title=>'Form Results', -author=>'BoChak'); #my $file=$cgi->param('file'); my $file="/root/Desktop/test.fasta"; print "<br> $file <br>"; ################ Process File ######################### #my $file=<>; my $seqname=''; my $sequence=''; my %seqhash=(); open (FILE,"<$file") or die "Can't open $file\n"; while (my $line=<FILE>){ chomp($line); if ($line=~m/^>/){ my @temp=split(' ',$line); $seqname=substr($temp[0],1); } elsif ($line=~m/^\s$/||$line=~m/^$/){ $seqhash{$seqname}=$sequence; } else { $sequence=$sequence.$line; } } close (FILE); ######################################################## foreach my $key (keys %seqhash){ my $risk=docParser->new($key, $seqhash{$key} ); $risk->CAG_count(); } print $cgi->end_html."\n";
docParser.pm package docParser; use strict; sub new { my $class = shift; my $self = bless {}, $class; my $seqname=shift; my $sequence=shift; $self->{_seqname}=$seqname; $self->{_sequence}=$sequence; return $self; } sub CAG_count { my ($self)=shift; my $seqname=$self->{_seqname}; my $sequence=$self->{_sequence}; my $count_of_CAG=0; my @seq=split(//,$sequence); my $aa=''; my @position; my $len; my $amino_acid_number; for (my $index=0; $index<$#seq-2; $index+=3) { if ($sequence=~m/((CAG){6,})/g){ $count_of_CAG++; @position=index($sequence,$1); $len=length($1)/3; $amino_acid_number=shift(@position); } } print "For the sequence ".$seqname." we found a CAG string "; print "with $len\n repeats "; print "at amino acid number ".$amino_acid_number."\n\n"; if ($len<35) { print "Risk Evaluation: Normal"."\n\n"; } elsif ($len>36||$len<48) { print "Risk Evaluation: Warning"."\n\n"; } elsif ($len>48) { print "Risk Evaluation: Dangerous"."\n\n"; } } 1;

Replies are listed 'Best First'.
Re: Passing a file from a form, processing, and then outputting as html
by gone2015 (Deacon) on Dec 13, 2008 at 12:17 UTC

    Scanning your code I note that I cannot see a use docParser anywhere in concepts2.pl... this may simply be because you missed out that bit ?

    You say that the problem is that "none of the printing is shown in the browser". If you had included a small sample input file I could have looked at this a bit more deeply...

    ...however, something apparently impossible is happening. All the output from the print statements in docParser.pm is getting lost. Whenever you come to a startling conclusion like that, step back from the problem. Look at it another way. Assume for a moment that your conclusion is wrong: ask yourself, why else would there be no output ? The foreach my $key (keys %seqhash) loop sends each captured sequence to the parser... what if there were no captured sequences ? OK, so that's impossible too -- because you're sure that piece of code works. BUT do not stop there: that is something you can verify very quickly by sticking a print statement or two before the foreach to show what the %seqhash contains...

    Looking at the "Process File" loop: (a) you can look for a blank line with just $line =~ m/^\s*$/; and (b) you need to consider what happens to $sequence during this loop. (Are you sure this bit works ?)

    I'm not sure why you are collecting the name and the sequence in a hash... If you do get two sequences with the same name, the second will replace the first -- this may be deliberate ? You won't get the sequences out in the same order they went in -- this may also be deliberate ?

Re: Passing a file from a form, processing, and then outputting as html
by mr_mischief (Monsignor) on Dec 13, 2008 at 04:29 UTC
    For one thing, you need to specify a non-default form encoding when posting files to the server. You need:
    <form action="cgi-bin/concepts2.pl" method="post" enctype="multipart/form-data">

    as your opening form tag. The default encoding type for HTML/HTTP forms is "application/x-www-form-urlencoded" rather than "multipart/form-data", but the latter is necessary for file upload.

    Also, whenever you don't see all the output you expect, try setting autoflush. Put $|++; in somewhere near the top of your code. This applies not only to CGI programming, but to any time you are Suffering from Buffering.

    Another tip to remember about CGI programming is that CGI::Carp is your friend, especially when you use it like this during debugging:

    use CGI::Carp qw( fatalsToBrowser );

    Be aware, though, that it gives some information away when it encounters a fatal error which could give an attacker helpful hints. Many people suggest using that feature only during development and not in production.

Re: Passing a file from a form, processing, and then outputting as html
by imrags (Monk) on Dec 13, 2008 at 06:50 UTC
    I was able to do something similar using this:
    print $query->start_multipart_form(-name=>'File_Form', -method=>'POST' +); print $query->p(("Enter the file to upload: "),$query->filefield(-name +=>'uploaded_file', -value=>''));
    Raghu
Re: Passing a file from a form, processing, and then outputting as html
by YesOn8 (Initiate) on Dec 13, 2008 at 05:10 UTC

    What i am trying to do is make a simple form, have an upload box for a file which contains data. This file is then processed via perl script, and then the results need to print out in the browser.

    sounds like a job for ajax

    http://www.w3schools.com/Ajax/Default.Asp

      It's possible to do such a thing. Yet there's no reason a plain old CGI can't accept a file upload, process it, and print out results.
      and also, w3schools is a great site, which is why i already checked there...