# Accessing an attribute inside a traditional object
$self->{ attribute } = $value;
# Accessing an attribute inside an inside-out object
$attribute { id $self } = $value;
####
package My::Person;
use Class::InsideOut qw[public readonly private register id];
# declare the attributes
readonly given_name => my %given_name;
readonly family_name => my %family_name;
public birthday => my %birthday;
public phone => my %phone;
private ssn => my %ssn;
public position => my %position;
# object constructor
sub new {
my $self = shift;
register( $self );
$given_name{ id $self } = shift;
$family_name{ id $self } = shift;
$birthday{ id $self } = shift;
$phone{ id $self } = shift;
$ssn{ id $self } = shift;
$position{ id $self } = shift;
}
##
##
use My::Person;
my $manager = My::Person->new(
'Random', 'Hacker',
'12/18/1987',
'555-1212',
'CEO'
);
print "Old phone is: ",$manager->phone();
$manager->phone('555-1313'); #set new phone number
print "New phone is: ",$manager->phone();
##
##
$manager->{phone} = '555-1313';
##
##
protection accessor_name => my %attribute_variable;
##
##
# $obj->public_attrib() accessor, $obj->public_attrib($value) mutator
public public_attrib => my %public_attrib;
# $obj->read_attrib() accessor, no mutator
readonly read_attrib => my %read_attrib;
# no accessor or mutator
private priv_attrib => my %priv_attrib;
##
##
sub sendPhoneToDirectory {
# sends the phone number to the company directory, using SSN as
# the key
my $self = shift;
my $dir = My::Directory->new();
my $entry = $dir->getEngtryBySSN( $ssn{ id $self } );
$entry->phone( $phone{ id $self } );
$entry->commit;
}
##
##
sub _set_birthday {
# remember, $_ will contain the value passed to the mutator
die "$_ is not a valid date" unless m{(\d{2})/(\d{2})/(\d{4})};
$_ = mktime(0,0,0,$2-1,$1-1,$3-1900); #pack it!
}
##
##
public birthday => my %birthday, { set_hook => \&_set_birthday };
##
##
sub _get_birthday {
# remember, $_ will contain the value of the attribute
strftime '%m/%d/%Y', localtime( $_ );
}
##
##
public birthday => my %birthday, {
set_hook => \&_set_birthday, # was already here
get_hook => \&_get_birthday,
};