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);
Have a cookie and a very nice day!
Lady Aleena

In reply to Web form to alter files is writing to file before submit button is pressed. by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.