package Person; use strict; use warnings; use Carp; use overload '""' => 'string'; =head1 Name Person.pm - A simple class to define a person =head1 Description This class defines a Person object. A person consists of a name and a social security number. =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__ =cut our $DEBUG = 0; #debugging turned off by default =head1 Subroutines The following subroutines are defined for Person objects =head2 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. =cut 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; } =head2 name Name accessor. Gets and Sets the name of the person. =cut sub name { my $self = shift; $self->{name} = shift if @_; return $self->{name}; } =head2 SSN Social Security Number accessor. Gets and Sets the SS# of the person. =cut 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}; } =head2 show Method to display a person. It shows the person's name and social secuity number. =cut sub show { my $self = shift; my ($name, $ssn) = ($self->{name}, $self->{SSN}); #format SS# properly $ssn =~ s/^(\d{3})(\d{2})(\d{3})$/$1-$2-$3/; print "$name has a SS# of $ssn\n"; } =head2 string Stringification overloading. Returned when a Person is used in a string context =cut sub string { my $self = shift; my ($name, $ssn) = ($self->{name}, $self->{SSN}); #format SS# properly $ssn =~ s/^(\d{3})(\d{2})(\d{3})$/$1-$2-$3/; return "$name ($ssn)"; } =head1 Copyright Copyright 2005, 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 =cut 1;