http://www.perlmonks.org?node_id=993944

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

I have some code where default values for textfields() are supplied from a database.

I have occasion to call the app with a different value, but I want the different value to be ignored and the default value from the database to be used.

I've tried setting it with -override in the textfield() as well as calling param() to set it specifically, but the output on the webpage is always the POST-supplied information instead of the -value=>"db data".

Any idea on how I can make it listen to me?

For now I have forced a redirect so it pulls data from the database (as there is no supplied data). It works fine, but it seems like a hack. It seems like there should be a way for the form value to be programmed, ignoring the POST parameter.

Replies are listed 'Best First'.
Re: perl CGI form ignores value
by rjt (Curate) on Sep 17, 2012 at 03:10 UTC

    There are several ways you can do this, but the following is reasonably efficient, and hopefully your parameter list is relatively small anyway:

    my $q = new CGI; my %vars = $q->Vars; # Fetch all CGI parameters my %db_vars = pop_defaults(); # Fetch from database # Override defined values from database $vars{$_} = $db_vars{$_} for (keys %db_vars); # Fetch all defaults from the DB and return as a hash ref sub pop_defaults { my $r = { }; ... return $r }

    You can also simply loop through the params:

    $vars{$_} = $db_vars{$_} // $q->param($_) for ($q->param);

    Finally, if you prefer to merge hash slices:

    %vars{keys %db_vars} = values %db_vars;
Re: perl CGI form ignores value
by Anonymous Monk on Sep 17, 2012 at 02:12 UTC

    It seems like there should be a way for the form value to be programmed, ignoring the POST parameter.

    You mean, like tell CGI.pm not to read any vars?

    Sure  CGI->new({}); It will still read cookies as those come via %ENV