Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:
I created a web form to help me alter my data files. However, the section of the script (lines 44-51) is altering the file on the first load of the script into my browser instead of waiting for me to press the Make changes (submit) button. Is there any way to tell it not to alter the file on the first load of the script? Any other suggestions to make this work better?
#!/usr/bin/perl use strict; use warnings; use CGI; use HTML::Entities qw(encode_entities); use lib 'lib'; use Base::HTML qw(start_html end_html); use Base::Roots qw(get_data); use Base::Data qw(get_hash); use Base::Nifty qw(line); my $file = get_data('Base','other_sites.txt'); my %sites = get_hash( file => $file, headings => [qw(name link)], ); my $cgi = CGI->new(); start_html; line(3,q(<form action="form_test.pl" method="get">)); line(4,q(<fieldset>)); line(3,q(<table>)); for my $site (sort {lc $a cmp lc $b} keys %sites) { my $name = $sites{$site}{name}; my $link = $sites{$site}{link}; $sites{$site}{new_name} = $cgi->param("$site\_name"); $sites{$site}{new_link} = $cgi->param("$site\_link"); line(4,q(<tr>)); line(5,qq(<td><input type="text" id="$site\_name" name="$site\_name" + value="$name"></td>)); line(5,qq(<td><input type="text" id="$site\_link" name="$site\_link" + value="$link"></td>)); line(4,q(</tr>)); } line(3,q(</table>)); line(5,q(<input type="submit" value="Make changes">)); line(4,q(</fieldset>)); line(3,q(</form>)); end_html; my @new_lines; for my $site (sort {lc $a cmp lc $b} keys %sites) { my $new_name = defined($sites{$site}{new_name}) ? $sites{$site}{new_ +name} : $sites{$site}{name}; my $new_link = defined($sites{$site}{new_link}) ? $sites{$site}{new_ +link} : $sites{$site}{link}; push @new_lines, "$new_name|$new_link"; } open(my $fh,'>',$file) or die "Can't open $file"; print $fh join("\n",@new_lines);
|
|---|