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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Passing a file from a form, processing, and then outputting as html

 



bobekdj
Novice

Dec 12, 2008, 8:08 PM

Post #1 of 16 (1726 views)
Passing a file from a form, processing, and then outputting as html Can't Post

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





Code
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>



Code
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";



Code
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;



KevinR
Veteran


Dec 12, 2008, 9:08 PM

Post #2 of 16 (1724 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

first problem that needs to be fixed is the form tag:


Code
<form action="cgi-bin/concepts2.pl" method="post">


to upload a file you need to add: ENCTYPE="multipart/form-data"


Code
<form action="cgi-bin/concepts2.pl" method="post" ENCTYPE="multipart/form-data">


then you can fix the script that gets the form data so it stores the uploaded file to disk. Then you can start to debug any problems with the parsing.

Take it one step at a time. Get the form corrected and make sure the file uplaod works. Then tackle other problems.
-------------------------------------------------


bobekdj
Novice

Dec 12, 2008, 9:10 PM

Post #3 of 16 (1722 views)
Re: [KevinR] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Yeah i fixed the form, now it says it cant open the file (which is what i expected), after adding the CGI::Carp command to print errors


KevinR
Veteran


Dec 12, 2008, 9:21 PM

Post #4 of 16 (1718 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post


In Reply To
Yeah i fixed the form, now it says it cant open the file (which is what i expected), after adding the CGI::Carp command to print errors


Change this line:


Code
open (FILE,"<$file") or die "Can't open $file\n";


change to:


Code
open (FILE,"<$file") or die "Can't open $file: $!\n";


$! will display the operating systems reason for not opening the file.
-------------------------------------------------


(This post was edited by KevinR on Dec 12, 2008, 9:23 PM)


bobekdj
Novice

Dec 12, 2008, 9:23 PM

Post #5 of 16 (1715 views)
Re: [KevinR] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Can't open test.fasta No such file or directory


KevinR
Veteran


Dec 12, 2008, 9:25 PM

Post #6 of 16 (1713 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post


In Reply To
Can't open test.fasta No such file or directory


Make sure this is the correct path and filename:


Code
my $file="/root/Desktop/test.fasta";


That is an unusual file path for a server but it could be correct for all I know.
-------------------------------------------------


bobekdj
Novice

Dec 12, 2008, 9:27 PM

Post #7 of 16 (1711 views)
Re: [KevinR] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

that is the location of the file on my fedora desktop, its not actually uploaded onto the server i dont think, so maybe there is an intermediate step i am missing, where the file goes from my desktop to the server...? What would the default upload directory be for temporarily uploaded files on an apache server?

btw, sorry for the PM, i noticed your sig after i sent it


KevinR
Veteran


Dec 12, 2008, 9:36 PM

Post #8 of 16 (1705 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post


In Reply To
that is the location of the file on my fedora desktop, its not actually uploaded onto the server i dont think, so maybe there is an intermediate step i am missing, where the file goes from my desktop to the server...? What would the default upload directory be for temporarily uploaded files on an apache server?

btw, sorry for the PM, i noticed your sig after i sent it


Theres the problem. The file path needs to be on the server the script is running on. Typically you tell the script where to store the file. If you don't know the path to your root www directory on the server you need to ask your tech support or IT department what it is.

You can also try printing this ENV variable to the browser:

print $ENV{'DOCUMENT_ROOT'};

it should display the path to your root www directory
-------------------------------------------------


bobekdj
Novice

Dec 12, 2008, 9:39 PM

Post #9 of 16 (1703 views)
Re: [KevinR] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Ok yeah, well then maybe i know more than i thought (although i am still a noob to this stuff). I know the www default directory, otherwise i couldnt be running and debugging this from localhost :-\, haha i should have mentioned that. Its all running on an apache server. Ok, well would you mind helping me with how i should "upload" the file, and then retrieve it?, or where, just to the www folder, or do i need to make a new folder in there and point the .pl file to it.


KevinR
Veteran


Dec 12, 2008, 9:48 PM

Post #10 of 16 (1701 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post


In Reply To
Ok yeah, well then maybe i know more than i thought (although i am still a noob to this stuff). I know the www default directory, otherwise i couldnt be running and debugging this from localhost :-\, haha i should have mentioned that. Its all running on an apache server. Ok, well would you mind helping me with how i should "upload" the file, and then retrieve it?, or where, just to the www folder, or do i need to make a new folder in there and point the .pl file to it.


I have an article and a script on another forum you can read:

http://bytes.com/topic/perl/insights/672398-how-upload-files-using-cgi-pm-module-perl

It does exactly what you need. I will be offline for a while but will check back in a few hours.

Regards,
Kevin
-------------------------------------------------


bobekdj
Novice

Dec 13, 2008, 9:34 AM

Post #11 of 16 (1688 views)
Re: [KevinR] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Thank you, i looked over that thread, and managed to upload the file (i think, your script said it was successful at least). Now the problem i am having is opening and processing the file, and then processing it with a class function, because i cant get the browser to show the print functions from the .pm file.


FishMonger
Veteran / Moderator

Dec 13, 2008, 11:33 AM

Post #12 of 16 (1679 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Personally, I'd take the print statements out of the CAG_count() sub. the sub should return either undef or a hash ref (or a list) of the desired results.


bobekdj
Novice

Dec 13, 2008, 11:36 AM

Post #13 of 16 (1677 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

I managed to get everything to work. Now the only thing i need to know, is how to insert this info into a database. I have everything stored as variables, but just need to know how to make a value (ID) insert as an auto increment.


FishMonger
Veteran / Moderator

Dec 13, 2008, 11:36 AM

Post #14 of 16 (1677 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

@position will never hold more that element, so why are you using an array?


KevinR
Veteran


Dec 13, 2008, 1:51 PM

Post #15 of 16 (1672 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Inserting the data into a databse is another issue and depends on the actual databse type you will be using. I am unfortunately very rusty in database /sql coding so I can't offer much help there. I know Fish is good with the sql stuff so maybe he can continue to assist you there if needed.
-------------------------------------------------


FishMonger
Veteran / Moderator

Dec 13, 2008, 2:02 PM

Post #16 of 16 (1671 views)
Re: [bobekdj] Passing a file from a form, processing, and then outputting as html [In reply to] Can't Post

Do you have the database setup, and if so, what is its schema?

Have you looked at the DBI (database interface) module?
http://search.cpan.org/~timb/DBI-1.607/DBI.pm

 
 


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

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