#!/usr/bin/perl use strict; use warnings; sub get_prompt { my $prompt = shift; print $prompt . ": "; my $input = ; chomp $input; return $input; } if ( ! @ARGV ) { print "Please give me something to do!\n"; exit 1; } my $command = shift @ARGV; if ( $command eq "add" ) { my $name = get_prompt "Full Name"; my $email = get_prompt "Email Address"; my $phone = get_prompt "Phone Number"; open my $savefile, ">>", "addressbook.db" or die "Couldn't open addressbook.db: ".$!; print $savefile $name . " %: " . $phone . " %: " . $email . "\n"; close $savefile; } elsif ( $command eq "list" ) { open my $loadfile, "<", "addressbook.db" or die "Couldn't open addressbook.db: $!"; while ( defined( my $line = <$loadfile> ) ) { chomp $line; my @parts = split / \%: /, $line; print "Name: " . $parts[0] . "\nPhone Number: " . $parts[1] . "\nEmail Address: " . $parts[2] . "\n\n"; } close $loadfile; } elsif ( $command eq "del" ) { my $target = get_prompt "Name to remove"; my $output = ""; open my $loadfile, "<", "addressbook.db" or die "Couldn't open addressbook.db: $!"; while ( defined( my $line = <$loadfile> ) ) { my @parts = split / \%: /, $line; if ( lc $parts[0] ne lc $target ) { $output .= $line; } } close $loadfile; open my $savefile, ">", "addressbook.db" or die "Couldn't open addressbook.db: ".$!; print $savefile $output; } else { print "I don't know how to " . $command . "\n"; exit 1; }