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


In reply to Re: More discipline or more freedom - in the context of OO by hardburn
in thread More discipline or more freedom - in the context of OO by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.