A Searchable Address Book (address.pl)

Our first script today consists of two parts:

  • A simple address book file, containing names, addresses, and phone numbers

  • A Perl script that prompts you for things to search for, and then prints out any matching addresses

This script makes use of just about everything you've learned so far this week: scalar and hash data, conditionals, loops, input and output, subroutines, local variables, and pattern matching. There's even a function call here and there to make things interesting. And so, without further ado, let's dive in.

How It Works

The address.pl script is called with a single argument: the address file, called address.txt. Call it on the command line as you have other Perl scripts:

% address.pl address.txt ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.