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

Im using the following to write to a config file, its just a hacked verdsion of the example on perldoc CGI. How do I clear the fields after I submit? Ive tried $new->new CGI; $new->delete_all(); and many other variations. Any Ideas?
#!/usr/bin/perl use CGI qw/:standard/; print header, start_html('Mothership Connection'), h1('Mothership Connection Configuration'), start_form, "Server Name ",textfield('name'),p, "IP Address ",textfield('ip'),p, "Port Number",textfield('port'),p, checkbox_group(-name=>'proto', -values=>['tcp','udp']),p, "Description",textfield('desc'),p, submit('submit'), end_form, hr; open (CFGFILE,">>mothership.cfg") || die; if (param()) { print CFGFILE (param('name')),",", (param('ip')),",", (param('port')),",", (param('proto')),",", (param('desc')), "\n"; } close CFGFILE;

Replies are listed 'Best First'.
Re: field deletion after a submit
by grep (Monsignor) on Apr 29, 2003 at 04:01 UTC
    As pzbagel stated you can use the the delete_all method to clear the CGI object's params using the OO interface. You can also use the functional interface, like you do in your example, to delete the params by using the Delete_all() function (note the leading cap).

    If you want to overide the "sticky" behavior on a per-field basis use -override.

    print textfield(-name=>'name', -default=>'', -override=>1 );


    grep
    Mynd you, mønk bites Kan be pretti nasti...
Re: field deletion after a submit
by pzbagel (Chaplain) on Apr 29, 2003 at 03:50 UTC

    First, start by defining a CGI object. Now the rest of the param and form element references in the script should be edited accordingingly with a $query->. Next move the data handling code to the top of your script and simply execute a $query->delete_all(); when you are done with it. This will clear your form data before it is printed again.

    Kinda like this:

    #!/usr/bin/perl use CGI qw/:standard/; $query= new CGI; if ($query->param()) { open (CFGFILE,">>mothership.cfg") || die; print CFGFILE (param('name')),",", ($query->param('ip')),",", ($query->param('port')),",", ($query->param('proto')),",", ($query->param('desc')), "\n"; close CFGFILE; $query->delete_all(); } print header, start_html('Mothership Connection'), h1('Mothership Connection Configuration'), $query->start_form, "Server Name ",$query->textfield('name'),p, "IP Address ",$query->textfield('ip'),p, "Port Number",$query->textfield('port'),p, $query->checkbox_group(-name=>'proto', -values=>['tcp','udp']),p, "Description",$query->textfield('desc'),p, $query->submit('submit'), $query->end_form, hr;
      Thanks! That helped a lot.