Should I learn Perl 5 OO or Moose first? [closed]

2019-03-11 14:25发布

I'm still relatively new to Perl Programming, but I know how Perl 5 OO basically works. However, I have never created any project with Perl 5 OO, so I'm quite sure I will run into many pitfalls.

Recently I discovered the hype about the Moose module. I checked out some documentation on CPAN and I found it to be quite interesting and helping me as a developer a lot. Additionally, it seems to be very stable and reliable.

Should I rather intensify working with the basic Perl 5 OO syntax until I feel very familiar with it (to know what's going on behind the stages), or do you think I should rather go ahead and directly start developing applications using Moose? Or should I even give Mouse a try?

Any thoughts and experiences on that are appreciated.

Thanks in advance!

11条回答
Melony?
2楼-- · 2019-03-11 14:57

Learn OOP that comes with Perl before Moose. This will make it a lot easier for you in the long run.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-03-11 15:00

I've started using Moose and like it quite a bit. I do agree with the other posts that say you should still learn how to do OO perl w/o Moose. But that tends to be difficult and there are many ways you can go about doing that. I think if you're starting a new project then Moose is the way to go.

I've also used Object::InsideOut, which is a lot like Moose and also helps protect your object variables from being tampered with.

Another note, I understand that Perl 6 objects will be much like Moose objects.. so learning Moose will prep you for Perl 6.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-11 15:01

IMHO I would learn Moose first. Why? Yes, most Perl OO is not done doing Moose. Yes, Moose is slow (though try Mouse). Yes, there's lots of practical reasons why you're going to have to eventually learn to do it the hard way. But there's one overriding reason.

Because Perl's way of doing OO warps your brain.

The point is to learn good OO, not Perl's OO. Once you understand OO programming as a concept them you can apply the technique to any specific language. The reverse is not so true.

Perl's stock OO doesn't give you much at all. You have to build all your pieces yourself. You have to learn all the little details of how everything works. It teaches you broken concepts like "objects are just magic hash references" and "methods are just subroutines with $self as the first argument" and "classes are just packages". In short, Perl OO teaches you to pay attention to how everything works which is the EXACT OPPOSITE of how OO is supposed to work.

OO is about NOT caring about the details of how things work. The whole point of an object is its a THING you ASK to do work and you don't care how it does it. A good object is like a good janitor. You ask the janitor to clean the floor, and then you walk away. When you come back, the floor is cleaned. It doesn't matter if the janitor used a mop, a toothbrush, their tongue or tore up the whole floor and installed a new one. The floor is clean and that's all that matters.

Furthermore, about the only way of composing objects that Perl gives you out of the box is inheritance. Inheritance is what everyone learns first when learning OO and its dangerous and mind warping. OO is OBJECT oriented, not inheritance oriented. The important thing is the encapsulation of the object, not that you can share code. A newbie programmer with inheritance is like giving a gun to the baby. Multiple inheritance is like giving them a howitzer. Newbies immediately leap to inheritance and created great tangled hierarchies. They never learn about delegation or composition or roles or mixins or any of the half dozen much better ways of letting objects share and build behaviors.

Moose gives you all that, out of the box, so you can focus on writing objects and not on writing an OO system.

Once you've learned how to do OO right then you can learn Perl's OO and how to do it twenty other ways, twelve of them wrong.

查看更多
我命由我不由天
5楼-- · 2019-03-11 15:04

Get comfortable with the basics first. Unless you've done a lot of OO JavaScript, OO Perl is going to seem a little strange, and some of the things that Moose or any other library does may seem wierd.

查看更多
6楼-- · 2019-03-11 15:07

This was going to be a comment on Schwern's post, but it grew.

I would say that moose is slower than "normal" Perl OO, but firstly this is not terribly important for most code (premature optimisation) and secondly if you do __PACKAGE__->make_immutable then a lot of the runtime overhead is removed anyway.

I'm with the "learn Moose first" gang. I like to remain willfully ignorant of Perl OO details for the kind of code that I write (data management and simple apps), and so I tend to use Moose for everything, even stuff I would have done procedurally in the past, because it makes a lot of the mechanics of the programming much easier

查看更多
别忘想泡老子
7楼-- · 2019-03-11 15:15

Most of the Perl world is not Moose, so you'll still need the basics to use all of the other modules.

查看更多
登录 后发表回答