package Person;
use strict;
use warnings;
use Carp;
use overload 
    '""' => 'string';

our $DEBUG = 0; #debugging turned off by default

sub new {
    #create a new Person
    my $class = shift;
    carp "No name and/or SS# passed in, using undef" unless @_ >= 2;
    my $self = { name=>$_[0], SSN=>$_[1] };
    bless $self, $class;
}

sub name {
    my $self = shift;
    $self->{name} = shift if @_;
    return $self->{name};
}

sub SSN {
    my $self = shift;
    if (@_){
	my $ssn = shift;
	carp "SS# not formatted correctly" unless $ssn =~ /^\d{3}-?\d{2}-?\d{4}$/;
	$self->{SSN} = $ssn;
    }
    return $self->{SSN};
}

sub show {
    my $self = shift;
    my ($name, $ssn) = ($self->{name}, $self->{SSN});
    #format SS# properly
    $ssn =~ s/^(\d{3})(\d{2})(\d{4})$/$1-$2-$3/;
    print "$name has a SS# of $ssn\n";
}

sub string {
    my $self = shift;
    my ($name, $ssn) = ($self->{name}, $self->{SSN});
    #format SS# properly
    $ssn =~ s/^(\d{3})(\d{2})(\d{4})$/$1-$2-$3/;
    return "$name ($ssn)";
}    

1;


__END__

=head1 Name

Person.pm - A simple class to define a person

=head1 Synopsis

 #!/usr/bin/env perl
 use strict;
 use warnings;
 use Person;
 
 my $p1 = new Person('John Smith', '016-32-9823');
 $p1->name('John Q. Smith');
 my $ss = $p1->SSN();
 
 $p1->show();
 print "Person 1: $p1\n";
 __END__

=head1 Description

This class defines a Person object.  A person consists of a name
and a social security number.

=head2 Subroutines

The following subroutines are defined for Person objects

=over 5

=item new

A constructor.  It takes two arguments: the name of the new person, and the new 
person's social security number.  It returns a Person object.

=item name

Name accessor.  Gets and Sets the name of the person.

=item SSN

Social Security Number accessor.  Gets and Sets the SS# of the person.

=item show

Method to display a person.  It shows the persons name and social secuity
number.

=item string

Stringification overloading.  Returned when a Person is used in a string context

=back

=head1 Copyright

Copyright 2009, Paul Lalli.  This file is freely distributed and may be used
or modified for any purposes.  No warranties are expressed or implied.

To contact the author, email lallip AT cs DOT rpi DOT edu


