text - Trouble recalling hashes from file in Perl -


this phone book script works in memory having difficult time recalling saved data on re execution. hashes go text file have no idea how recall them when script starting. have used "storage" save data, , have tried use "retrieve"function bring data back, no luck. think either did not follow path start or don't know in code or %hash should "retrieve" stored data.

i new perl , programming hope explained situation clearly

#!/usr/bin/perl  use 5.18.2; use strict; use warnings; use autodie; use scalar::util qw(looks_like_number); # used determine if phone number entered valid. use storable; use data::dumper;  #################################### # enables sub selections %contact; while (){     $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 use sub list{     print "\n------------------------------------------------------------------------\n";     print "-                     ----- select option -----                     -\n";      print "- 1 = add, 2 = remove, 3 = find, 4 = list, 5 = tidy screen, 888 = quit -\n";     print "------------------------------------------------------------------------\n";     print "what do? ";     $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 letters lower-case     if (exists $contact{$addcontactname}){ # checks duplicate contact         print"duplicate record!!! please enter 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 numbers             $contact{$addcontactname} = $phonenumber; # adds hash contact #               open (file, ">>pb.txt"); # file save contact info #               print file $addcontactname .= ":", $phonenumber .= "\n"; # add colon , new line contact info in text file         }         else{             print "phone numbers not have letters!, let's start again.\n\n"; # displays error message         }     } }  #################################### # sub remove contact sub removecontact {     print"enter name remove: \n";     chomp (my $removecontact = <stdin>); # enter contact name remove     $removecontact = lc($removecontact); # changes letters lower-case     if (exists $contact{$removecontact}){ # looks contact name         delete($contact{$removecontact}); # delete contact name , info         print"the contact \' $removecontact \' has been removed\n"; # gives confirmation of contact removal         }      else {                 print"this name not exist in record!! try again.\n\n"; # displays error message         } }  #################################### # sub find contact sub findcontact {     print"whom looking for?: \n";     chomp(my $findcontact = <stdin>); # enter contact name find     $findcontact = lc($findcontact); # changes letters lower-case     if (exists $contact{$findcontact}) { # looks contact name         print($contact{$findcontact},"\n\n"); # prints info found contact name     }      else {         print"this name not exist in record!!! try again.\n\n"; # displays error message     } }  ############################################### # lists contacts entered alphabetically sub listallcontacts {     $key (sort keys %contact) { # sorts contacts alphabetically      print "$key, $contact{$key}\n"; # shows contacts on screen     } }  ################################################# # tidy sub - clears screen of clutter sub clearscreen {     system("clear"); }  #################################### # sub leave program sub quit{         store (\%contact, "pb.txt"); # save data text file     system("clear"); # clears screen     exit(); # exits program } 

the store function comes module storable (you can see full documentation module typing perldoc storable).

it's counterpart called retrieve.

so in order read contacts on script start can replace line

my %contact; while (){ 

with

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

the eval makes retrieval not die on error (whether because pb.txt not there or not contain data in format compatible storable). might want have more elaborate error handling instead of ignoring errors, should example.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -