- or download this
# Accessing an attribute inside a traditional object
$self->{ attribute } = $value;
# Accessing an attribute inside an inside-out object
$attribute { id $self } = $value;
- or download this
package My::Person;
...
$ssn{ id $self } = shift;
$position{ id $self } = shift;
}
- or download this
use My::Person;
...
print "Old phone is: ",$manager->phone();
$manager->phone('555-1313'); #set new phone number
print "New phone is: ",$manager->phone();
- or download this
$manager->{phone} = '555-1313';
- or download this
protection accessor_name => my %attribute_variable;
- or download this
# $obj->public_attrib() accessor, $obj->public_attrib($value) mutator
public public_attrib => my %public_attrib;
...
# no accessor or mutator
private priv_attrib => my %priv_attrib;
- or download this
sub sendPhoneToDirectory {
# sends the phone number to the company directory, using SSN as
...
$entry->phone( $phone{ id $self } );
$entry->commit;
}
- or download this
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!
}
- or download this
public birthday => my %birthday, { set_hook => \&_set_birthday };
- or download this
sub _get_birthday {
# remember, $_ will contain the value of the attribute
strftime '%m/%d/%Y', localtime( $_ );
}
- or download this
public birthday => my %birthday, {
set_hook => \&_set_birthday, # was already here
get_hook => \&_get_birthday,
};