1

这个电话簿脚本在内存中运行良好,但我很难在重新执行时调用保存的数据。哈希转到文本文件,但我不知道如何在脚本启动时调用它们。我使用“存储”来保存数据,并尝试使用“检索”功能将数据带回,但没有运气。我认为要么我从一开始就没有遵循一个好的路径,要么我只是不知道代码在哪里或者哪个 %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
}
4

1 回答 1

2

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

它的对应物称为retrieve.

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

my %contact; while (){

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

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

于 2016-02-06T21:59:07.650 回答