#!/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!

$_->[0]:$_->[1]! } @data; print < $html HTML } sub get_file { my $file = shift; open FILE, $file or die_nice("Could not open $file, Perl says $!"); my @lines = ; 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 { "$_
" } @deletions; print "

Deleted these entries:\n

$html"; } sub die_nice { my $error = shift; print "Sorry an error occurred: $error"; exit; }