in reply to More discipline or more freedom - in the context of OO
I think it'd be hard to find a language that can't use OO at all. You could probably do it in BrainF*ck if you tried hard enough.
Though it might not be as deliberatly difficult as BF, Perl actually has a pretty high barrier to reach before you can start creating objects (this is different from using its object system, since it's not that hard to write my $obj = Foo->new(); $obj->bar(1);). To actually create objects in Perl, you have to understand (at a minimum) subroutines, references, packages, the bless builtin, and probably a few other things I can't think of ATM. In Java, you've already created your first class upon writing a "Hello, World!" (albeit with a lot of handwaving on the part of the tutorial author).
That's just Perl's most common object system. The concepts you need to grasp for using more esoteric Perl OO probably extend to the entire bredth of Perl knowledge.
So Perl gives us a high learning curve before we can start creating classes and objects. The trick is that once we've survived all that, we have an extremely powerful tool.
Raise your hand if you'd like to write a bunch of code like this:
int foo, bar, baz; int get_foo() { return foo; } int set_foo(int foo) { this.foo = foo; } int get_bar() { return bar; } int set_bar(int bar) { this.bar = bar; } int get_baz() { return baz; } int set_baz(int baz) { this.baz = baz; }
Or would you rather try this:
foreach my $field (qw( foo bar )) { no strict 'refs'; *$field = sub { my $self = shift; $self->{$field} = shift if @_; $self->{$field}; }; }
Even if you split the get/set into two subroutines, the second provides a far easier way to add new attributes that need nothing more than a basic accessor/mutator. This is just one example of how Perl OO is far nicer to program for than Java.
There are a lot of things that bug me about Perl OO. Lacking a common way to do prototypes, Perl's subroutines can be trickier to use, and lack a nice self-documenting property. This, and most of my other greviances, is already covered in the Perl6 Apocolypses.
One other point:
This structure, let’s call it self structure is passed to each function (method) as the first parameter, so now all the function (method) knows about all properties;
This doesn't work very well with inheirtance. Arguably, inheirtance is over-stressed in OO, but I still think it's an important and very powerful concept.
----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
Note: All code is untested, unless otherwise stated
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: More discipline or more freedom - in the context of OO
by tilly (Archbishop) on Oct 15, 2003 at 03:09 UTC |