Posts Tagged ‘objects’

Just as Steve Knight, I don’t find much use for object oriented programming in my own code. But there is a name for methods (or functions or subroutines) that drag a bit of state around and that is Object Based Programming. And it is really handy.

What does it get you?

If you have a few variables that need to be operated on together in multiple methods, you can stuff them in the same object.

sub new
    my ($class, $dbh, $person) = @_;
    my $self = { dbh => $dbh, person => $person, }
    bless $self, $class;
    return $class;

You can use accessor methods to do lazy initialisation.

sub person
    my $self = shift;
    if (! exists $self->{person}) {
        $self->{person} = Person->new();
    return $self->{person};

And given a half decent language you can perform tidy-up in the destructor (which, as a C++ fiddler, I kinda like).


Read Full Post »