package My::Class; use warnings; use strict; use Carp qw(croak); sub new { my ($class, %args) = @_; my $self = bless {%args}, $class; $self->_validate_args; return $self; } sub swear { my ($self, $word) = @_; croak "swear() needs word param sent in" if ! defined $word; if ($self->can_swear) { print "$word\n"; } } sub can_swear { # getter/setter, and returns the value of the 'can_swear' attribute. # This attribute determines whether the object is allowed to use foul language my ($self, $can_swear) = @_; $self->{can_swear} = $can_swear if defined $can_swear; # If can_swear wasn't set via this method, or wasn't set # within the %args parameters hash to new(), we default # to not allowing the object to swear return $self->{can_swear} // 0; } sub _validate_args { my ($self) = @_; if (! exists $self->{not_provided_in_new_args}) { # do stuff } } #### use My::Class; my %args = ( swear => 1, emphasis => 1, }; my $object = My::Class->new(%args); $object->swear('damnit'); #### $object->swear('damnit'); #### swear($object, 'damnit'); #### $VAR1 = [ bless( {}, 'My::Class' ), 'damnit', ];