filehandles

Discussion in 'Perl Misc' started by superfly2, Jun 16, 2004.

  1. superfly2

    superfly2 Guest

    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.
    superfly2, Jun 16, 2004
    #1
    1. Advertising

  2. superfly2 wrote:
    > 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?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 16, 2004
    #2
    1. Advertising

  3. superfly2

    Brad Baxter Guest

    On Tue, 15 Jun 2004, superfly2 wrote:

    > 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
    Brad Baxter, Jun 16, 2004
    #3
  4. superfly2

    superfly2 Guest

    "Brad Baxter" <> wrote in message
    news:p...
    > On Tue, 15 Jun 2004, superfly2 wrote:
    >
    > > 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
    superfly2, Jun 16, 2004
    #4
  5. superfly2

    Ben Morrow Guest

    Quoth "W. Citoan" <>:
    > On Tue, 15 Jun 2004 18:06:25 -0700, superfly2 wrote:
    > > "Brad Baxter" <> wrote in message
    > > news:p...
    > > >
    > > > Perhaps because your script never opens them the files? :)

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

    >
    > That statement doesn't match the comment in your code:
    >
    > > > > print DEBUG $file1; #prints filenames correctly
    > > > > print DEBUG $file2;


    Read the docs for CGI.pm.

    Ben

    --
    If I were a butterfly I'd live for a day, / I would be free, just blowing away.
    This cruel country has driven me down / Teased me and lied, teased me and lied.
    I've only sad stories to tell to this town: / My dreams have withered and died.
    (Kate Rusby)
    Ben Morrow, Jun 16, 2004
    #5
  6. superfly2

    Brad Baxter Guest

    On Tue, 15 Jun 2004, superfly2 wrote:

    > "Brad Baxter" <> wrote in message
    > news:p...
    > >
    > > Perhaps because your script never opens them the files? :)


    s/them //;

    > >

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

    ....
    > > > my $file1 = $cgi->param('filex');
    > > > my $file2 = $cgi->param('filey');


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

    Regards,

    Brad
    Brad Baxter, Jun 16, 2004
    #6
  7. superfly2

    Sam Holden Guest

    On Tue, 15 Jun 2004 22:10:22 -0400, Brad Baxter <> wrote:
    > On Tue, 15 Jun 2004, superfly2 wrote:
    >
    >> "Brad Baxter" <> wrote in message
    >> news:p...
    >> >
    >> > Perhaps because your script never opens them the files? :)

    >
    > s/them //;
    >
    >> >

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

    > ...
    >> > > my $file1 = $cgi->param('filex');
    >> > > my $file2 = $cgi->param('filey');

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

    --
    Sam Holden
    Sam Holden, Jun 16, 2004
    #7
  8. superfly2

    Brad Baxter Guest

    On Tue, 16 Jun 2004, Sam Holden wrote:

    > On Tue, 15 Jun 2004 22:10:22 -0400, Brad Baxter <> wrote:
    > >
    > > 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.


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

    Brad
    Brad Baxter, Jun 16, 2004
    #8
  9. superfly2

    Sam Holden Guest

    On Wed, 16 Jun 2004 09:46:37 -0400, Brad Baxter <> wrote:
    > On Tue, 16 Jun 2004, Sam Holden wrote:
    >
    >> On Tue, 15 Jun 2004 22:10:22 -0400, Brad Baxter <> wrote:
    >> >
    >> > 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.

    >
    > 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...)

    --
    Sam Holden
    Sam Holden, Jun 17, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Phillips

    newbie - Output to multiple Filehandles

    Jeremy Phillips, May 24, 2004, in forum: Perl
    Replies:
    1
    Views:
    2,126
    Jürgen Exner
    May 25, 2004
  2. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    1
    Views:
    628
    Jürgen Exner
    Jul 10, 2004
  3. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    598
    Andrew
    Jul 10, 2004
  4. Andrew

    FileHandles to string

    Andrew, Jul 10, 2004, in forum: Perl
    Replies:
    0
    Views:
    648
    Andrew
    Jul 10, 2004
  5. Sako
    Replies:
    3
    Views:
    5,595
Loading...

Share This Page