bless is good parts of Perl language

Recently I feel bless is good parts of Perl language strongly.

package Point;

use strict;
use warnings;

sub new {
my $class = shift;

my $self = {@_};

return bless $self, $class;

sub x { shift->{x} }
sub y { shift->{y} }

This is smallest class which have only read accessors. Very clean and compact.

This class work well completely.

use Point;
my $point = Point->new(x => 1, y => 2);
my $x = $point->x;

Some people think Perl default OO system is very bad. They hate bless and don't teach bless to newbie.

Because of many prejudices and political reason, did not we lose sight of simple and clean things?


I believe you are embracing what many of us see as Perl's greatest strength but others see as a weakness. Its brevity and simple treatment of blessed objects to produce a workable (if not elegant) OO framework allows the best of both worlds - a flexible scripting language and a platform for some higher order designs - if you so desire.

I find Perl's expressiveness and beauty no less beautiful because of an incomplete OO model. Moreover, while others pine for more advanced OO-edness and have crafted some rather impressive things out of Perl 5, I'm good with bless too. ;-)

I do not see Perl's object model as incomplete, merely as different. If object models were automobiles, Perl's "bless" model would be an old Chevy with no hood, chromed air cleaner and valve covers, and a candy apple paint job with flames on the sides. The lack of a hood does not make the Chevy an "incomplete car" -- it still does everything you need a car to do. I feel it is just the same with Perl. The "bless" model does everything I need it to do. In fact, because it actually exposes object creation in ways that traditional O-O models do not, I can do more with Perl than with, say, Java.

For example, in a traditional O-O language "new" is part of the syntax. When you use it it manufactures an object of the given class, executes an initializer, and hands you the object. In Java, if I want a Runtime object I can't do new Runtime() because Runtime is an abstract class and can not be instantiated. I have to call static method Runtime.getRuntime(), which hands me an OS-specific subclass. But in Perl new() is a static method. If Java had Perl's object model I could get a Runtime object the same way I got any other.

Leave a comment

About Yuki Kimoto

user-pic I'm Perl Programmer. I LOVE Perl. I want to contribute Perl community and Perl users.