Connecting Tech Pros Worldwide Help | Site Map

Deleting Records Part 3

By Blair Ireland
Senior Editor, TheScripts.com

Now that we know what fields we are going to delete, its time to actually delete them.

elsif ($form{'action'} eq "delete_three") {
    &delete_three;
}

This part tells us we are using sub delete_three, so lets have a look.

sub delete_three {
    my (@keys) = split (/,/,$form{'key'});

This requires some explanation beforehand. At the top of our script, the sub procedure &parse is called, and the results are put into the hash %form. &parse takes all input fields submitted and puts them into their own key in the hash, the key being the name of the form field. When &parse encounters multiple form fields submitted with the same name, it separates each value with a ','. Therefore, since the checkboxes in the previous sub procedure (&multi_match) all have the name 'key', we split $form{'key'} by these commas, to get each different value placed in the array @keys.

    my ($output) = "";
    my ($found) = 0;

$output and $found are both declared at my variables, and set to null values

    open (DATABASE, $file);

The database file is opened with a handle of DATABASE, in read-only mode

    while (<DATABASE>) {

This loop executes for the whole DATABASE file. It stops after you reach EOF, or end of file.

        chomp($_);

Since each line in DATABASE is referred to as $_, this rids each line of the extra newline at the end.

        (/^\s*$/) and next;

If the line is blank, it is skipped

        my (%dat) = &process_record(&grab_data($_));

$_ is changed into an array, and that array is processed in &process_record, making it a hash. It is returned into the hash %dat, which is set as a my variable.

        foreach $key (@keys) {

Remember that list of keys we split into @keys on the first line of this sub? Well, here is where we go over it, with each value in @keys referred to as $key.

            if ($dat{$db_key} eq $key) {
                $found = 1;
            }

Since we set $db_key as 'ID' at the top of the program, this looks in $dat{'ID'} to see if it matches $key. If it does, we set $found to 1, a non null value. Think of it in binary terms. 0 is off, 1 is on.

        }

This is the end of that foreach loop.

        if ($found) {
            $found = 0;
            next;
        }

If $found is a non null value (or 'on' in binary terms), we execute this if block. It resets $found to a null value, and uses the control statement, next. This takes us to the next iteration of the while loop, skipping this line entirely.

        else {
            $output .= "$_\n";
        }

Otherwise, if $found is a null value, you add to $output the value of the current line, not forgetting to put the newline back at the end of the line.

    }

End of while loop

    close (DATABASE);

This closes our read-only copy of DATABASE

    open (DATABASE, ">".$file);

Time to re-open DATABASE, but this time its in write-only mode. The entire contents of the database is wiped out right here.

    if ($flock) {
        flock(DATABASE, 2);
    }

If we set $flock to a non-null value at the top of the script, File Locking is used (flock). The DATABASE part tells perl to set the lock on the file with the DATABASE handle, and to a value of 2, which actually sets the lock.

    print DATABASE $output;

Remember how we put data not being deleted into $output? Here we print it. All the newlines and formatting already exists, so just print it.

    close (DATABASE);

DATABASE is finally closed, automatically removing the file lock. All specified database entries have now been deleted.

    &header("Record Edited Successful");
    print qq~<CENTER><font size=-1>Record(s) Deleted></CENTER>~;
    &footer;

The sub &header is called with the title, and some HTML is printed, then the sub &footer is printed.

}

End of sub delete_three.Excellent! You just finished the deleting routines of the database..... That means only 1 measly section is left. Modifying data.

« Deleting Records Part 2 Modifying Records Part 1 »