in reply to looking towards learning OOP

Thanks for asking this question as I'm (almost) in the same boat. I have some training in Java but rely on Perl for most of what I do. I want to do a re-write of a Perl project from procedural to OO that's becoming increasingly unwieldy and difficult to maintain and expand. Before I start though I've been trying to understand the best way to go about learning OO in Perl and one that I think makes sense.

To me, native Perl OO (blessing an anonymous hash ref) doesn't make sense. You get no encapsulation (and hence potential namespace conflicts) and get none of the benefits of strict and warn since you're storing data according to keys, not hard-coded variables. This is crazy, completely insane! I desperate want strict and warn when I'm trying to make more wieldy code out of my large data structures, and especially when I'm trying to learn how to do it.

So I started investigating inside-out objects, a clever hack on top of Perl's native OO that encapsulates instance data with compile-time checking, just the way I want it. However I got a number of suggestions before settling on Class::InsideOut as the best potential solution (but not without significant reservation). Then I got suggested Moose as a nice OO framework. Moose on the surface looks good but to me it's a huge black box and not part of the standard distro, so my code becomes that much less portable (which is a goal).

So you guess I could say I'm confounded by the TMTOWTDI nature of it all - with objects, seriously, I only need one paradigm that looks and feels like Perl. At this point I'm ready to throw up my hands and just use the native Perl method to see if it's worth all the fuss to avoid it, although I know if anything should go awry I may regret my decision. I look forward to weighing all the suggestions in this thread.

Replies are listed 'Best First'.
Re^2: looking towards learning OOP
by spx2 (Deacon) on Sep 12, 2008 at 21:49 UTC
    my problem with Moose is that its documentation becomes obsolete
    as time passes by and it is spread(presentations,articles,cookbooks,cpan docs) all over the internet.
    noone seems to understand that for it to be attractive for developers
    it needs to have nicely binded documentation in one place and one place only.
    if I go to #moose people are nice, but it is not nice for me to come knocking on
    their door each time I hit a bump in the road just because noone took the trouble
    to nicely bind and write a good documentation for the project.
    and if documentation isn't enough or well written or any at all ,
    they suggest to read the source code,but that would be an effort for my side
    so big,that it would mean not only I am trying to learn Moose but also it's internals,
    the latter of which was not my intention at all.

    as a sidenote:
    if I want to learn c++ it doesn't require me to understand the internals and/or read it's
    source code(altough on some occasions it is useful)I just need to read the description of what each class does,it's methods,some
    examples and I can start using it
      Yes, there's presentations and tutorials spread all over the Internet, but the official, always up-to-date, documentation is on the CPAN here. In that documentation are a set of tutorials called the Cookbook.

      There isn't much to learn about basic Moose:

      package Shape; use Moose; has color => ( # an attribute called 'color' is => 'rw', # creates an accessor for the color isa => 'Str', # and it's a string ); package Circle; use Moose; extends 'Shape'; # Circle isa Shape has radius => ( is => 'rw', isa => 'Num' ); sub area { my $self = shift; return 3.141 * $self->radius ** 2 } package Rectangle; use Moose; extends 'Shape'; # Rectangle isa Shape has width => ( is => 'rw', isa => 'Num' ); has height => ( is => 'rw', isa => 'Num' ); sub area { my $self = shift; return $self->width * $self->height; } package main; use strict; use warnings; my $r = Rectangle->new(height => 2, width => 2.5, color => 'green'); my $c = Circle->new(radius => .5, color => 'yellow'); printf "My %s is %s and has an area of %0.2f\n", ref $_, $_->color, $_ +->area for $r, $c; $c->radius(1); # The circle is now twice the size printf "My new Circle is %s and has an area of %0.2f\n", $c->color, $c +->area

      Moose just makes it so quick and easy. Of course, Moose can do so much more, but that's what the documentation is for.

      (Yes. I know. Roles would have been better)


      Unless I state otherwise, all my code runs with strict and warnings

      About not wanting to "bother" #moose with your beginner's issues:

      Look at it the other way around. You're a beginner and trying to learn things. You read the documentation and there's something you don't get. You ask #moose. You get a good answer. Here's what you have to do to make #moosee helping you benefit Moose: Apply small fixes to the documentation as you go along!

      I think it's Matt Trout who's been saying over and over again that as an expert, you can't reasonably write good beginner's documentation because you have no idea what the hell's so difficult about it. Conversely, if there's going to be a beginner friendly text, beginners have to be involved in the making.

      Best regards,
      Steffen

      my problem with Moose is that its documentation becomes obsolete as time passes by and it is spread(presentations,articles,cookbooks,cpan docs) all over the internet

      I disagree, if it is not in the CPAN Moose docs, then it is here, and if it not in either of those place then I don't know about it and so can't link to it. Also I am not sure how the spread and growth of information on something causes the core docs to become obsolete? If anything the core docs should always be thought of as definitive.

      noone seems to understand that for it to be attractive for developers it needs to have nicely binded documentation in one place and one place only.

      Actually, we understand that all to well, but it is a matter of people finding time. Personally i think the recently re-organized cookbooks as well as the new Moose::Intro and Moose::Unsweetened docs go a long way towards filling some of the needs you describe.

      Of course documentation always needs work, both keeping it up to date and expanding on things which are not covered as well as they could be. You simply have to stop by #moose or ask on moose@perl.org and we will be happy to give you a commit bit and you can start fixing it right away.

      as a sidenote: if I want to learn c++ it doesn't require me to understand the internals and/or read it's source code(altough on some occasions it is useful)I just need to read the description of what each class does,it's methods,some examples and I can start using it

      This is not in any way a fair comparison.

      C++ is over 20 years old (1985 or so) has the support of several major corporations (AT&T, IBM, Microsoft, etc), is an ANSI and ISO standard and got millions of dollars pumped into training, books and other forms of documentation over the last 15-20 years.

      Moose is just 2 1/2 years old and is a volunteer driven open source project with absolutely no funding, grants or anything of the like.

      -stvn