I've been working on a script using CGI.pm for which I need the 'nosticky' option. That is:
-nosticky
By default the CGI module implements a state-preserving behavior called "sticky" fields. The way this works is that if you are regenerating a form, the methods that generate the form field values will interrogate param() to see if similarly-named parameters are present in the query string. If they find a like-named parameter, they will use it to set their default values.
Sometimes this isn't what you want. The -nosticky pragma prevents this behavior. You can also selectively change the sticky behavior in each element that you generate.
I've seen a number of posts here and elsewhere complaining about it, and after a few hours of frustration I finally decided to dig through the code to figure out what was going wrong. As far as I can tell, the nosticky option does almost nothing. It's only referenced by the submit method, and has no effect even there if you supply a label for the submit button.
The override option, on the other hand, does work. Unfortunately, you have to pass it to every output method instead of setting it globally. I wrote up a small test script to demonstrate the problem, and I've hosted a
copy here
#!/usr/bin/env perl
use strict;
use warnings;
use CGI qw/-nosticky :standard/;
print header, html(
start_form,
checkbox(
-nosticky => 1,
-name => "checkbox",
-label => "Broken (using nosticky)",
-checked => 0
),
br,
checkbox(
-override => 1,
-name => "checkbox",
-label => "Working (using override)",
-checked => 0
),
br,
submit,
end_form
);
If you check both boxes and click Submit, the second one will correctly be cleared, but the first one will stay checked. It seems really odd that a feature that's been around for so long in such a prominent module could have been broken all this time, so I still feel like I must be doing something wrong, but I can't see what it would be.