All pastes #26258 Raw Edit

Unnamed

public text v1 · immutable
#26258 ·published 2005-10-22 09:31 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use warnings;
sub get_prompt {
        my $prompt = shift;
        print $prompt . ": ";
        my $input = <STDIN>;
        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[1] . "\nPhone Number: " . $parts[2] . "\nEmail Address: " . $parts[3] . "\n\n";
        }
}
else {
        print "I don't know how to " . $command . "\n";
        exit 1;
}