filehandles

S

superfly2

Hi,

I'm getting the following error, but I don't know why this would happen
given my script.

readline() on unopened filehandle at /bin/apache/cgi-bin/termCompare.pl line
96., referer: http://[host]/biotools/termCompare.shtml
(and one more just like it)

My script is:

use LWP::UserAgent;
use HTTP::Cookies;
use CGI;

use strict;


open(DEBUG, ">/bin/apache/cgi-bin/compareDebug.txt");

# Read in POST data and put in variables

#$CGI::pOST_MAX = 512 * 1024; #limit to 512kb upload
my $cgi = new CGI;
my $area1 = $cgi->param('areax');
my $area2 = $cgi->param('areay');
my $file1 = $cgi->param('filex');
my $file2 = $cgi->param('filey');
my $submit_method = $cgi->param('submit');

print DEBUG $file1; #prints filenames correctly
print DEBUG $file2;

my(@list1, @list2);
if($submit_method eq 'Submit Text')
{
$area1 =~ s/\r/\n/g;
$area1 =~ s/;/\n/g;
$area1 =~ s/\t/\n/g;
$area1 =~ s/(\n+)/\n/g;
$area2 =~ s/\r/\n/g;
$area2 =~ s/;/\n/g;
$area2 =~ s/\t/\n/g;
$area2 =~ s/(\n+)/\n/g;

# Now put into arrays the data stuff
@list1 = split(/\n/, $area1);
@list2 = split(/\n/, $area2);
}
else
{
my $term;
while($term = <$file1>) # error refers to this line
{
chomp $term;
print DEBUG "* $term\n";
push @list1, $term;
}
while($term = <$file2>) # error refers to this line
{
chomp $term;
print DEBUG "+ $term\n";
push @list2, $term;
}
}

# if i print list1 or list2, there is nothing in them.
 
G

Gunnar Hjalmarsson

superfly2 said:
I'm getting the following error, but I don't know why this would
happen given my script.

readline() on unopened filehandle at
/bin/apache/cgi-bin/termCompare.pl line 96., referer:
http://[host]/biotools/termCompare.shtml
(and one more just like it)

Do you have

enctype="multipart/form-data"

in the form element?

Have you tried upload() instead of param() for getting the filehandles?
 
B

Brad Baxter

Hi,

I'm getting the following error, but I don't know why this would happen
given my script.

readline() on unopened filehandle at /bin/apache/cgi-bin/termCompare.pl line
96., referer: http://[host]/biotools/termCompare.shtml
(and one more just like it)

Perhaps because your script never opens them the files? :)
My script is:

use LWP::UserAgent;
use HTTP::Cookies;
use CGI;

use strict;


open(DEBUG, ">/bin/apache/cgi-bin/compareDebug.txt");

You should always, yes ALWAYS, check the return value from open.
Hi, Tad. :)

(But that's not your problem here ...)

# Read in POST data and put in variables

#$CGI::pOST_MAX = 512 * 1024; #limit to 512kb upload
my $cgi = new CGI;
my $area1 = $cgi->param('areax');
my $area2 = $cgi->param('areay');
my $file1 = $cgi->param('filex');
my $file2 = $cgi->param('filey');
my $submit_method = $cgi->param('submit');

print DEBUG $file1; #prints filenames correctly
print DEBUG $file2;

my(@list1, @list2);
if($submit_method eq 'Submit Text')
{
$area1 =~ s/\r/\n/g;
$area1 =~ s/;/\n/g;
$area1 =~ s/\t/\n/g;
$area1 =~ s/(\n+)/\n/g;
$area2 =~ s/\r/\n/g;
$area2 =~ s/;/\n/g;
$area2 =~ s/\t/\n/g;
$area2 =~ s/(\n+)/\n/g;

# Now put into arrays the data stuff
@list1 = split(/\n/, $area1);
@list2 = split(/\n/, $area2);
}
else
{
my $term;
while($term = <$file1>) # error refers to this line

Whoa, you can't read a file this way. First, you open it, then you read
it. See perldoc -f open

[snippage]

Regards,

Brad
 
S

superfly2

Brad Baxter said:
Hi,

I'm getting the following error, but I don't know why this would happen
given my script.

readline() on unopened filehandle at /bin/apache/cgi-bin/termCompare.pl line
96., referer: http://[host]/biotools/termCompare.shtml
(and one more just like it)

Perhaps because your script never opens them the files? :)

That is not true. $file1 and $file2 should be open filehandles.

My script is:

use LWP::UserAgent;
use HTTP::Cookies;
use CGI;

use strict;


open(DEBUG, ">/bin/apache/cgi-bin/compareDebug.txt");

You should always, yes ALWAYS, check the return value from open.
Hi, Tad. :)

(But that's not your problem here ...)

# Read in POST data and put in variables

#$CGI::pOST_MAX = 512 * 1024; #limit to 512kb upload
my $cgi = new CGI;
my $area1 = $cgi->param('areax');
my $area2 = $cgi->param('areay');
my $file1 = $cgi->param('filex');
my $file2 = $cgi->param('filey');
my $submit_method = $cgi->param('submit');

print DEBUG $file1; #prints filenames correctly
print DEBUG $file2;

my(@list1, @list2);
if($submit_method eq 'Submit Text')
{
$area1 =~ s/\r/\n/g;
$area1 =~ s/;/\n/g;
$area1 =~ s/\t/\n/g;
$area1 =~ s/(\n+)/\n/g;
$area2 =~ s/\r/\n/g;
$area2 =~ s/;/\n/g;
$area2 =~ s/\t/\n/g;
$area2 =~ s/(\n+)/\n/g;

# Now put into arrays the data stuff
@list1 = split(/\n/, $area1);
@list2 = split(/\n/, $area2);
}
else
{
my $term;
while($term = <$file1>) # error refers to this line

Whoa, you can't read a file this way. First, you open it, then you read
it. See perldoc -f open

[snippage]

Regards,

Brad
 
B

Brad Baxter

s/them //;
That is not true. $file1 and $file2 should be open filehandles. ....

Sorry, but param() doesn't return open filehandles. I guess you just need
to open them yourself.

Regards,

Brad
 
S

Sam Holden

s/them //;


Sorry, but param() doesn't return open filehandles. I guess you just need
to open them yourself.

Yes it does. I guess you need to read the documentation.

Of course using upload() instead of param() is a far wiser choice.
 
B

Brad Baxter

Yes it does. I guess you need to read the documentation.

Of course using upload() instead of param() is a far wiser choice.

Sunny beaches. I stand corrected--just never used that feature, and
didn't recall reading about it. Thanks.

Brad
 
S

Sam Holden

Sunny beaches. I stand corrected--just never used that feature, and
didn't recall reading about it. Thanks.

This is the main reason upload() is to be prefered (in my opinion, anyway)
it documents that the thing is a file handle and not just a piece of text.
It also makes error checking easier (for people who send garbage to the
script, such that the file upload field isn't a file upload) - but for me
the documentation aspect is the reason to use it (though grepping
some code, I see I didn't always...)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top