Listing 2

#!/usr/local/bin/perl

use X11::Xforms;
use strict;
no strict "refs";
use vars qw($FILE $DATA $list $name $browser
            $name_field $phone_field
            $address1_field $address2_field
            $email_field);
require "pbook.pl";

$FILE = "$ENV{HOME}/.pbook"; #Where to store data

#----------
# MAIN CODE
#----------

fl_initialize('Phone Book');
create_form_list();
load_data();
update_browser();
fl_show_form($list, FL_PLACE_FREE, FL_FULLBORDER,
                                'Phone Book');
while(1) {fl_do_forms()};

#--------------------------------
#  Subroutines Go here....
#--------------------------------

sub load_data {    #READ IN DATA FILE, AND PARSE IT
    open(IN,"$FILE");
    while(<IN>) {
        chomp;
        my ($name,$pnumber,$add1,$add2,$email)
            = split /:/,$_;
        $DATA->{$name}={
                    name    => $name,
                    pnumber => $pnumber,
                    add1    => $add1,
                    add2    => $add2,
                    email    => $email
                    }
    }
    close IN;
}

    #SAVE DATA, AND MAKE A BACKUP COPY

sub save_data {
    rename($FILE,"$FILE.bak");
    open OUT, ">$FILE";
    foreach $name (sort keys %$DATA) {
        my @fields = ($name,
            map {"$DATA->{$name}->{$_}"}
                qw(pnumber add1 add2 email)
        );
        print OUT (join ':', @fields) . "\n";
    }
    close OUT;
}

    #PUT DATA INTO BROWSER LIST

sub update_browser {
    fl_clear_browser($browser);
    foreach $name (sort keys %$DATA) {
        fl_add_browser_line($browser, $name);
    }
}

    #UPDATE THE DATA FROM THE ENTRY LINES

sub update_data {
    my $name = fl_get_input($name_field);
   my $new_entry = $DATA->{$name} ? 0 : 1;
    $DATA->{$name} = {
      name    => $name,
      pnumber => fl_get_input($phone_field),
      add1    => fl_get_input($address1_field),
      add2    => fl_get_input($address2_field),
      email   => fl_get_input($email_field)
    };
    update_browser() if ($new_entry);
    save_data();
}

#UPDATE THE ENTRY LINES FROM THE BROWSER SELECTION

sub browser_clicked    {
    my $name = fl_get_browser_line($browser,
                    fl_get_browser($browser));
    fl_set_input($name_field,$name);
    fl_set_input($phone_field,
                $DATA->{$name}->{pnumber});
    fl_set_input($address1_field,
                $DATA->{$name}->{add1});
    fl_set_input($address2_field,
                $DATA->{$name}->{add2});
    fl_set_input($email_field,
                $DATA->{$name}->{email});
}

#CLEAR ALL THE ENTRY FIELDS

sub clear_data    {
    for (qw(name phone address1 address2 email)) {
        fl_set_input(${$_.'_field'},'');
    }
}

#REMOVE AN ENTRY, UPDATE THE BROWSER TO
#REFLECT IT, THEN SAVE DATA

sub delete_entry    {
    $name=fl_get_browser_line($browser,
                fl_get_browser($browser));
    delete $DATA->{$name};
    update_browser();
    save_data();
}

sub quit {
    exit(0);
}