use strict; my (%PhoneBook,$TheCaller); my ($ValidTest,$InvalidTest); my ($name,$NumToFind); # Read in CSV phone book &ReadPhoneBookFile; # Run two parameter tests $ValidTest = "8954"; $InvalidTest = "9999"; print "Parameter test 1 - Number to find is: $ValidTest\n"; $TheCaller = &MatchPhoneNumber($ValidTest); print "The caller was: $TheCaller\n"; print "\nParameter test 2 - Number to find is: $InvalidTest\n"; $TheCaller = &MatchPhoneNumber($InvalidTest); print "The caller was: $TheCaller\n"; # Run two direct tests. # First one works, second one doesn't. print "\nDirect test 1 (8594) - ".$PhoneBook{8594}; # WORKS! $NumToFind = "8594"; print "Direct test 2 (8594) - ".$PhoneBook{$NumToFind}; # DOES NOT WORK! exit; sub ReadPhoneBookFile ### ### Reads in a CSV separated phone book, splits, stuffs details into a hash ### ### In real-world script, data is read from an external file. { my ($Name,$Number,@Details); foreach () { @Details = split(/,/,$_); $Number = $Details[0]; $Name = $Details[1]; $PhoneBook{$Number} = $Name; } } sub MatchPhoneNumber ### ### Attempts to match the phone number with a name from the phonebook ### Either returns the caller name if number in hash or "Unknown caller". ### { my $NumToFind = shift; my $TheName; $TheName = defined $PhoneBook{$NumToFind} ? $PhoneBook{$NumToFind} : 'Unknown Caller'; return $TheName; } __DATA__ 8594,Tom 9000,Dick 1234,Harry