Let me tell you two (?) anonymous monks what I want to ultimately accomplish with this script which may help you point me in the right direction. I should have done that initially, but as usual, I was a little shortsighted. I have made some small changes to the script since the original post, so I will post the updated script at the end of this post.

  1. Load the form into the browser with the data in text fields for editing.
  2. After editing the data, save the data to the same file it came from.
  3. Reload the form with the updated data for possible further editing.

So far, with the few changes made, I am able to accomplish the first and second items on the list. The changes are that I changed the method from get to post and added a conditional to the code at the end to check if there were any changes made. I have not gotten to the third item yet, but I am still thinking of ways to accomplish it.

For the future, I am going to make a subroutine or subroutines out of this so that I can use it for all of my data editing needs.

So, if you see anything else I need to change, your help is always appreciated.

#!/usr/bin/perl use strict; use warnings; use CGI; use File::Basename; 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 $form = basename($0); 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,qq(<form action="$form" method="post">)); 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(5,q(<input type="button" onclick="location='$form'" value="Start +over">)); line(4,q(</fieldset>)); line(3,q(</form>)); end_html; if ($cgi->param) { my @new_lines; for my $site (sort {lc $a cmp lc $b} keys %sites) { my $new_name = defined($sites{$site}{new_name}) ? $sites{$site}{ne +w_name} : $sites{$site}{name}; my $new_link = defined($sites{$site}{new_link}) ? $sites{$site}{ne +w_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 Re^6: Web form to alter files is writing to file before submit button is pressed. by Lady_Aleena
in thread 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.