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

Fellow monks,

I've read through the POD for CGI.pm and I'm still confused on how to save the state of a form.

I've got the following code running at present. The checkboxes are being built from a hash table.

sub printboxes{ foreach my $key ( sort keys %list ){}; print header; print start_html('Comics Configuration Page'), start_form, $query->checkbox_group(-name=>'comics', -values=>[grep {!/html?$/} sort %list +], -columns=>3), submit(-name=>'Save my choices!'), end_form, br, print end_html; open FH, '>'.$file; $query->save('FH'); close FH; }

My assumption was, after reading the POD, that I could save the state of the form ( which boxes were checked ) but when I look at the file it produces, I've got one equals sign there, so it seems that it isn't picking up anything. I've also seen the filehandle written as 'FH', FH, and \*FH and have tried all these variants and still no luck.

If anybody can help with this I'd greatly appreciate it!

There is no emoticon for what I'm feeling now.

Replies are listed 'Best First'.
Re: Saving state in cgi.pm question
by tadman (Prior) on Nov 25, 2002 at 05:20 UTC
    This should be what you're intending:
    if (open($fh, ">", $file)) { $query->save($fh); close($fh); } else { # Die? Complain? }
    The documentation indicates that any filehandle is valid. Since passing older-style (pre-5.6?) filehandles is tricky, using the newer method is easier. That's $fh instead of FH
Re: Saving state in cgi.pm question
by Zaxo (Archbishop) on Nov 25, 2002 at 05:27 UTC

    You appear to have a either a closure or global variables on $query, $file, and %list. Under various conditions you may get unexpected results. It is better practice to pass values to subroutines as arguments.

    sub printboxes { my ($query, $file, %list) = @_; # ... open FH, '> ' . $file or die $!; $query->save(FH) or die $!; close FH or die $!; }
    I added some error checking on the save operations, so you get better information if something there is the problem.

    Update: The data you get saved suggests that $query does not have any data in it. Could be the kind of unexpected behavior I mentioned. Or do your test runs neglect to provide data?

    After Compline,
    Zaxo

      Thanks for the response, but I'm still having the same problem. I can write the file fine, but all it contains is one '=' sign and that's it. I have tried checking off some of the boxes ( saving the state *should* be storing my selections shouldn't it? ) but I'm not getting any actual data saved.

      Any ideas in that direction?

      Unless I am misunderstanding how the saving of the state works, I can't see a need to process the data if you can save it. Also, does the save happen after the submit button is pushed?

      Thanks!

      There is no emoticon for what I'm feeling now.

Re: Saving state in cgi.pm question
by LTjake (Prior) on Nov 25, 2002 at 12:57 UTC
    After a short chat with Zaxo, we both agree that the save function will only save name-value pairs. Thus, it will only work with a POST or GET query with params, *OR* if you create the params yourself (ex: $query->param(-name=>'thing',-value=>'val');). Trying to save the state of a form when it is first created will not work unless you create the params yourself.

    The example in the docs validates this assumption.

    HTH

    --
    Rock is dead. Long live paper and scissors!
Re: Saving state in cgi.pm question
by UnderMine (Friar) on Nov 25, 2002 at 13:06 UTC
    If you are looking at storing data in persiant form of use later :-
    use Storable qw{freeze thaw};
    are worth keeping in mind.

    Hope it helps
    UnderMine

Re: Saving state in cgi.pm question
by Starky (Chaplain) on Nov 26, 2002 at 06:56 UTC
    While this doesn't answer your question directly, as your needs grow from saving state to maintaining state via sessions etc. you may want to check out Apache::PageKit, which automagically provides all these things in a mod_perl environment. The documentation is a bit sparse, but once you get comfortable with the framework, it is a godsend.

    You may also want to take a look at HTML::FillInForm which will allow you to later prepopulate an HTML form with the state you saved earlier. HTML::FillInForm is used by PageKit, so if you use the latter you get the former for free.

    Hope this helps :-)