在 Perl 中从文件中召回哈希值时遇到问题

Trouble recalling hashes from file in Perl

这个 phone 书籍脚本在内存中运行良好,但我很难在重新执行时回忆起保存的数据。哈希转到文本文件,但我不知道如何在脚本启动时调用它们。我已经使用 "Storage" 保存数据,并且尝试使用 "retrieve" 函数将数据取回,但没有成功。我认为要么我从一开始就没有遵循好的路径,要么我只是不知道代码中的哪个位置或哪个 %hash 应该 "retrieve" 存储的数据。

我对 Perl 和编程还很陌生,所以我希望我能清楚地解释我的情况

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

store 函数来自模块 Storable(您可以通过键入 perldoc Storable 查看该模块的完整文档)。

它的对应项称为 retrieve

因此,为了在脚本开始时读回联系人,您可以替换行

my %contact; while (){

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

eval 使检索不会因错误而死(无论是因为 pb.txt 不存在还是不包含与 Storable 兼容的格式的数据)。您可能希望有更详细的错误处理,而不是仅仅忽略任何错误,但这应该作为示例。