Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Perl 5 OOP solutions

by xdg (Monsignor)
on Apr 14, 2006 at 20:16 UTC ( [id://543434]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl 5 OOP solutions
in thread Perl 5 OOP solutions

Using 'extends', you can safely subclass. It doesn't guarantee much (yet) in the way subclassing non-Moose stuff, but

But that is, unfortunately, exactly the issue. Almost every class framework in Perl works fine as long as you consistently use that framework.

Can Moose be (safely) subclassed without extends? Can Moose subclass a non-Moose class? If so, I applaud highly. But, as you say, it's still 0.03_02... there's a long way to go until the OP's questions are fully addressed.

If you can show me how Moose could be used to subclass, say, File::Marker, then I'll be a believer, but short of that, I think it's a nice wrapper around all the old problems.

It's a very nice wrapper and will work for most people for most things, I'd bet, but it's not a fundamental change in the way that I think inside-out objects are. (And as I said, those should be used with caution too.)

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^5: Perl 5 OOP solutions
by stvn (Monsignor) on Apr 14, 2006 at 23:13 UTC
    But that is, unfortunately, exactly the issue. Almost every class framework in Perl works fine as long as you consistently use that framework.

    One of the major design goals of Moose is to "play well with others". So far at $work have successfully integrated it with CGI::Application, or course we can't utilize all that Moose has to offer (attribute handling and instance construction to name a few), but we have found lots of use for Moose's method modifiers. The work with DBIx::Class and Catalyst is all fairly new, but so far has seems to be going smoothly.

    Can Moose be (safely) subclassed without extends?

    Let me first clarify that in order to use Moose you do not subclass it. Much like you module Class::InsideOut it exports functions into the class's namespace to provide it's features. In fact Class::InsideOut actually inspired me to use this approach in Moose. Now, onto the questions:

    All extends does is to set the @ISA, the reason it uses extends and not base is to make sure that all Moose classes inherit from Moose::Object unless otherwise specified (through extends). I won't bore you with the details, but just know that extends is pretty much just optional sugar.

    Can Moose subclass a non-Moose class?

    This, as you very well know, is tricky given the many possible different instance structures Perl 5 OO offers. The short answer is yes, Moose can do this (and we do it with our Moose-ified CGI::Application). However, things get tricky when you need to deal with the details of instance construction. Currently both Moose and Class::MOP only support blessed hashes, but this is because that is what I use, and not a restriction of these modules so much (although changing Moose over at this early stage would be pretty sticky). Now, onto the "how" of this question:

    The (fairly common) idiom of calling $class->SUPER::new and then reblessing is reworked in Moose to look like this:

    my $super_instance = $class->SUPER::new(@_); my $self = $class->meta->new_object(__INSTANCE__ => $super_instance, @ +_);
    Ugly I know, but it does the trick if you are just dealing with blessed hashes. It is no better or worse than the non-Moose version IMO. From this point on in your class hierarchy you can use Moose normally, in other words, it doesn't ripple upwards.

    If you can show me how Moose could be used to subclass, say, File::Marker, then I'll be a believer, but short of that, I think it's a nice wrapper around all the old problems

    Well, if we need to add state, then Moose can't do that. This is because File::Marker is an inside-out object which wraps a blessed GLOB reference (an IO::File instance), and thats just too many different modes of instance storage to deal with.

    However, if you only need to add or augment behavior, and not state. Moose and it's method modifiers (before, after & around) could prove quite useful.

    It's a very nice wrapper and will work for most people for most things, I'd bet, but it's not a fundamental change in the way that I think inside-out objects are.

    That is actually exactly the goal of Moose, to not require a fundamental change in the way you do Perl 5 OO, but instead to try and expand on the existing model.

    -stvn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://543434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found