简体   繁体   English

在Perl中无法从文件中调用哈希

[英]Trouble recalling hashes from file in Perl

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. 我认为或者我从一开始就没有走好的路,或者我只是不知道代码在哪里,或者哪个%hash应该“检索”存储的数据。

I am very new to Perl and programming so I hope I explained my situation clearly 我对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
}

The store function comes from the module Storable (you can see full documentation for the module by typing perldoc Storable ). store功能来自Storable模块(您可以通过输入perldoc Storable来查看该模块的完整文档)。

It's counterpart is called retrieve . 它的对应对象称为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 ). eval使检索不会因错误而pb.txt (无论是因为pb.txt不存在还是不包含与Storable兼容的格式的数据)。 You might want to have somewhat more elaborate error handling instead of just ignoring any errors, but this should do as an example. 您可能希望有一些更详尽的错误处理,而不是仅仅忽略任何错误,但这仅作为示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM