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

OK, this one is driving me nuts. Here's the deal:

... ... ... open (QUERY,'<query.txt'); my $q = new CGI(\*QUERY); close QUERY; ... ... open (QUERY, '>query.txt'); $q->save(\*QUERY); print $q->header(); ... ...
The above code does not work. It refuses to save my form parameters in the 'query.txt' file. I'm getting no errors. Now here is the kicker: the below simpler code saves my form's contents just fine:
... ... ... my $q = new CGI; ... ... open (QUERY, '>query.txt'); $q->save(\*QUERY); print $q->header(); ... ...
Notice that the only difference between the two codes is that the second does not attempt to restore the parameters. Of course, with the above code I can't restore my parameters, either. I've been at this quite a while and my nerves are getting frayed. Any help is appreciated.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar";
$nysus = $PM . $MCF;

Replies are listed 'Best First'.
Re: Saving and storing form params with cgi.pm
by Beatnik (Parson) on May 25, 2001 at 13:19 UTC
    try:
    open (QUERY, '>query.txt') || die $!; $q->save(QUERY); close(QUERY); print $q->header();
    I did something similar recently, and it worked fine...

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: Saving and storing form params with cgi.pm
by iakobski (Pilgrim) on May 25, 2001 at 12:31 UTC
    I ran your code on Active Perl 5.6 and it worked fine. Have you tried it with the intervening stuff taken out? Also the man page says to pass the filehandle directly rather than by glob reference:
    $query->save(FILEHANDLE)
    But it worked both ways when I tried it.

    -- iakobski

      Ditto here on Linux 2.2, Perl 5.005_03, and Active State 5.6. Works fine.
Re: Saving and storing form params with cgi.pm
by nysus (Parson) on May 25, 2001 at 16:44 UTC
    Here's the entire sample code:
    #!/usr/bin/perl -w use strict; use CGI qw(:html3); open ('QUERY', '<query.txt') || die "Cannot open: $!"; my $q = new CGI ('QUERY'); close QUERY; BEGIN { use CGI::Carp qw(carpout fatalsToBrowser); open (LOG, ">errorlog.txt") or die ("Unable to open errorlog.txt: $!\n"); carpout(\*LOG); } open (QUERY, '>query.txt') || die "Cannot open: $!"; $q->save('QUERY'); print $q->header(); print $q->start_html(); print $q->startform("post",'eventform.cgi'); print $q->textfield(-name=>'field_name', -size=>50, -maxlength=>80); print $q->submit(-name=>'button_name', -value=>'value'); print $q->endform; print $q->end_html(); close QUERY;

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar";
    $nysus = $PM . $MCF;

      I don't get it. Your program does...

      1. read parameters from a file,
      2. write them back to the same file,
      3. display a form.

      The stuff entered in that form will never be processed.

      what exactly are you trying to do?

      --
      Brigitte    'I never met a chocolate I didnt like'    Jellinek
      http://www.horus.com/~bjelli/         http://perlwelt.horus.at

      Many, many thanks to arturo for his patience in helping me figure this out

      I had to create to query objects. Not knowing OO, I don't quite understand what's going on here but it works. Here is the code that works:
      #!/usr/bin/perl -w use strict; use CGI qw(:html3); open ('QUERY', '<query.txt') || die "Cannot open: $!"; my $q = new CGI ('QUERY'); my $p = new CGI; close QUERY; BEGIN { use CGI::Carp qw(carpout fatalsToBrowser); open (LOG, ">errorlog.txt") or die ("Unable to open errorlog.txt: $!\n"); carpout(\*LOG); } print $q->header(); print $q->start_html(); print $q->startform("post",'eventform.cgi'); print $p->textfield(-name=>'field_name', -size=>50, -maxlength=>80); print $q->submit(-name=>'button_name', -value=>'value'); print $q->endform; print $q->end_html(); open (QUERY, '>query.txt') || die "Cannot open: $!"; $p->save('QUERY'); close QUERY;

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar";
      $nysus = $PM . $MCF;

        OK, I'm assuming that what you want this script to do is maintain state in between invocations, and to allow users to change that state by submitting the form. Your code isn't (yet) able to do that. There are a couple of things that aren't clear to me, such as how exactly you expect this script to be accessed (can users stay on the form and keep resubmitting? I take it that's what you want to allow). But here's how I'd do the basic state-maintenance thing; note there's no code, because, as bjelli points out, the problem all along was with your program logic, which tells me that's what you need to think about first here.
        1. Get the parameters that have been submitted via HTTP.
        2. If the parameter set is blank, read the state in from the file. (using the method you're using)
        3. If the parameter set came from a form submission and not from the file, save the current set of parameters to the file.
        4. Present the form, with each field containing the relevant value from the parameter set that we have.

        If that's not what you wanted to do, then you'll need to be more explicit.

        The idea here is that when users come to your CGI for the first time, they'll receive the saved state. If they then submit the form, what they submit will get saved to the file.

        HTH