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

Hi all experts, I had written a code as follows.
#!/usr/bin/perl print "Content-type: text/html\n\n"; open (HANDLE,"/usr/fweb/catch/yy.txt"); @datasinfile = <HANDLE>; close(HANDLE); foreach $say (@datasinfile) { @sur = split(/\n/,$say); push(@sur1,@sur); } foreach(@sur1) { my($val1,$val2)= split(/:/,$_); push(@val,$val1); } print <<ht; <html><head></head><body><form name="del" method="POST" action="/cg-bi +n/test3.pl"> ht ; $i=0; foreach $v(@val) { $var="<input type=\"checkbox\" name=\"chk$i\" value=\"$v\">$v<br>"; $i++; print $var; } print <<ht1; <input type="Submit" name="delete" value="delete"> </form></body><html> ht1 ; yy.txt file format --------------------- one:pass two:fail three:success four:average
Execute this code and if i choose some values and click on the delete button then i need to modify the text file. Please provide a solution to handle this problem.

Edit: davorg added code tags

Replies are listed 'Best First'.
Re: Text file updation
by tachyon (Chancellor) on Dec 27, 2002 at 12:51 UTC

    Well seeing that it is XMAS and all, this should get you going:

    #!/usr/bin/perl -w use strict; my $script = "http://localhost/cgi-bin/test.pl"; my $file = "/tmp/data.txt"; use CGI; my $q = new CGI; print $q->header(); if ( $q->param('delete') ) { my @deletions = modify_file(); show_done_page(@deletions); show_form(); } else { show_form(); } exit; #### SUBS #### sub show_form { my @lines = get_file($file); # remove newlines; chomp (@lines); my @data = map { [ split ':', $_ ] } @lines; my $html = join "\n", map {qq!<p><input type="checkbox" name="dele +te" value="$_->[0]">$_->[0]:$_->[1]! } @data; print <<HTML; <form name="del" method="POST" action="$script"> $html <input type="Submit" name="Submit" value="Delete"> HTML } sub get_file { my $file = shift; open FILE, $file or die_nice("Could not open $file, Perl says $!") +; my @lines = <FILE>; close FILE; return @lines; } sub write_file { my $file = shift; open FILE, ">$file" or die_nice("Could not write $file, Perl says +$!"); print FILE @_; close FILE; } sub modify_file { my @deletions = $q->param('delete'); my $re = join '|', map { quotemeta } @deletions; $re = qr/^($re):/; my @lines = get_file($file); my @ok_lines = grep { not /$re/ } @lines; write_file($file, @ok_lines); return @deletions; } sub show_done_page { my @deletions = @_; my $html = join "\n", map { "$_<br>" } @deletions; print "<p>Deleted these entries:\n<p>$html"; } sub die_nice { my $error = shift; print "Sorry an error occurred: $error"; exit; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      If anything more sophisticated to the text file needs to be done, one could consider using DBD::CSV or even DBI and DBD::CSV.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      tachyon, Thanks... Your code is useful for me. Thanks
Re: Text file updation
by ColtsFoot (Chaplain) on Dec 27, 2002 at 11:37 UTC
    Try using CODE tags around your code samples it does help to make the post a lot more readable.
    Also use the -w flag on the shebang line and ALWAYS use strict;
    Have a look at the CGI.pm module it will be your friend

    Hi all experts, I had written a code as follows.
    #!/usr/bin/perl print "Content-type: text/html\n\n"; open (HANDLE,"/usr/fweb/catch/yy.txt"); @datasinfile = <HANDLE>; close(HANDLE); foreach $say (@datasinfile) { @sur = split(/\n/,$say); push(@sur1,@sur); } foreach(@sur1) { my($val1,$val2)= split(/:/,$_); push(@val,$val1); } print <<ht; <html><head></head><body><form name="del" method="POST" action="/cg-bi +n/test3.pl"> ht ; $i=0; foreach $v(@val) { $var="<input type=\"checkbox\" name=\"chk$i\" value=\"$v\"$v"; $i++; print $var; } print <<ht1; <input type="Submit" name="delete" value="delete"> </form></ body><html> ht1 ; yy.txt file format --------------------- one:pass two:fail three:success four:average
    Execute this code and if i choose some values and click on the delete button then i need to modify the text file. Please provide a solution to handle this problem.