无法从Perl中的文件中调用哈希值

时间:2016-02-06 21:27:57

标签: perl text hash storable

这个电话簿脚本在内存中运行良好,但我很难回忆起重新执行时保存的数据。散列转到文本文件但我不知道如何在脚本启动时调用它们。我用过" Storage"为了保存数据,我试图使用" retrieve"函数来恢复数据,没有运气。我想要么我从一开始就没有遵循一条好路,或者我只是不知道代码中的哪个位置或哪些%hash应该"检索"存储的数据。

我是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
}

1 个答案:

答案 0 :(得分:2)

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

它的对应部分称为retrieve

因此,为了回读脚本上的联系人,您可以替换行

my %contact; while (){

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

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