Trouble recalling hashes from file in Perl

102 Views Asked by At

This phone book script works well in memory but I am having a difficult time recalling the saved data on re execution. The hashes go to the text file but I have no idea how to recall them when the script is starting. I have used "Storage" to save the data, and I have tried to use the "retrieve"function to bring the data back, with no luck. I think either I did not follow a good path from the start or I just don't know where in the code or which %hash should "retrieve" the stored data.

I am very new to Perl and programming so I hope I explained my situation clearly

#!/usr/bin/perl

use 5.18.2;
use strict;
use warnings;
use autodie;
use Scalar::Util qw(looks_like_number); # This is used to determine if the phone number entered is valid.
use Storable;
use Data::Dumper;

####################################
# Enables sub selections
my %contact; while (){
    my $selection = list();

    if ($selection == 1){
        addContact();
    }
    elsif ($selection == 2){
        removeContact();
    }
    elsif ($selection == 3){
                findContact();
    }
    elsif ($selection == 4){
        listAllContacts();
    }
    elsif ($selection == 5) {
                clearScreen();
        }
    elsif ($selection == 888) {
        quit();
    }
    else {
        print "Invalid entry, Please try again.\n\n"; # displays error message
    }
}
####################################
# Shows instructions for use
sub list{
    print "\n------------------------------------------------------------------------\n";
    print "-                     ----- Select an option -----                     -\n"; 
    print "- 1 = add, 2 = remove, 3 = find, 4 = list, 5 = tidy screen, 888 = quit -\n";
    print "------------------------------------------------------------------------\n";
    print "What would you like to do? ";
    my $listChoice = <STDIN>; # enter sub choice here
    return $listChoice;
}

####################################
# Add contact info sub
sub addContact{
    print"Name?: ";
    chomp (my $addContactName = <STDIN>); # contact name
    $addContactName = lc($addContactName); # changes all letters to lower-case
    if (exists $contact{$addContactName}){ # checks for duplicate contact
        print"Duplicate Record!!! Please enter a different name\n\n"; # displays error message
    } 
    else {
        print"Phone Number?(omit dashes, ex. 1235551212): ";
        chomp (my $phoneNumber = <STDIN>); # phone number
                if (looks_like_number($phoneNumber)){ # checks that its only numbers
            $contact{$addContactName} = $phoneNumber; # adds hash to contact
#               open (FILE, ">>pb.txt"); # file to save contact info
#               print FILE $addContactName .= ":", $phoneNumber .= "\n"; # add a colon and new line to contact info in text file
        }
        else{
            print "Phone Numbers do not have letters!, Let's start again.\n\n"; # displays error message
        }
    }
}

####################################
# sub to remove contact
sub removeContact {
    print"Enter name to remove: \n";
    chomp (my $removeContact = <STDIN>); # enter contact name to remove
    $removeContact = lc($removeContact); # changes all letters to lower-case
    if (exists $contact{$removeContact}){ # looks for contact name
        delete($contact{$removeContact}); # delete contact name and all info
        print"The contact \' $removeContact \' has been removed\n"; # gives confirmation of contact removal
        } 
    else {
                print"This name does not exist in the record!! Try Again.\n\n"; # displays error message
        }
}

####################################
# sub to find a contact
sub findContact {
    print"Whom are you looking for?: \n";
    chomp(my $findContact = <STDIN>); # enter contact name to find
    $findContact = lc($findContact); # changes all letters to lower-case
    if (exists $contact{$findContact}) { # looks for contact name
        print($contact{$findContact},"\n\n"); # prints info for found contact name
    } 
    else {
        print"This name does not exist in the record!!! Try Again.\n\n"; # displays error message
    }
}

###############################################
# Lists all contacts entered alphabetically
sub listAllContacts {
    for my $key (sort keys %contact) { # sorts contacts alphabetically 
    print "$key, $contact{$key}\n"; # shows all contacts on screen
    }
}

#################################################
# Tidy sub - just clears the screen of clutter
sub clearScreen {
    system("clear");
}

####################################
# sub to leave the program
sub quit{
        store (\%contact, "pb.txt"); # save data to text file
    system("clear"); # clears screen
    exit(); # exits program
}
1

There are 1 best solutions below

1
On BEST ANSWER

The store function comes from the module Storable (you can see full documentation for the module by typing perldoc Storable).

It's counterpart is called retrieve.

So in order to read back the contacts on the script start you can replace the line

my %contact; while (){

with

my %contact;
eval {
    %contact = %{ retrieve "pb.txt" };
};
while (1) {

The eval makes the retrieval not die on error (whether because pb.txt is not there or does not contain the data in a format compatible with Storable). You might want to have somewhat more elaborate error handling instead of just ignoring any errors, but this should do as an example.