Moose OOP 还是标准 Perl?

发布于 2024-11-16 12:13:32 字数 272 浏览 8 评论 0原文

我要为一个网站编写一些爬虫,这个想法是该网站将使用一些后端 Perl 脚本从其他网站获取数据,我的设计(以一种非常抽象的方式..)将是编写一个包,可以说:

package MyApp::Crawler::SiteName

其中站点名称将是用于爬行特定站点的模块/包,我显然会有其他包在不同模块之间共享,但这里不相关。

无论如何,长话短说,我的问题是:为什么(或者为什么不......)我应该更喜欢 Moose 而不是 Standard OO Perl?

I'm going into writing some crawlers for a web-site, the idea is that the site will use some back-end Perl scripts to fetch data from other sites, my design (in a very abstract way..) will be to write a package, lets say:

package MyApp::Crawler::SiteName

where site name will be a module / package for crawling specific sites, I will obviously will have other packages that will be shared across different modules, but that not relevant here.

anyway, making long short, my question is: Why (or why not...) should I prefer Moose over Standard OO Perl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

将军与妓 2024-11-23 12:13:34

使用 Moose。这是我昨晚写的东西(在本例中使用 鼠标)。它做什么、验证什么以及设置什么应该非常明显。现在想象一下编写等效的原始 OO。这不是超级难,但它确实开始变得更难阅读,不仅仅是代码本身,而且当阅读你以前或一段时间没有见过的代码时,意图可能是最重要的部分。

has "io" =>
    is => "ro",
    isa => "FileHandle",
    required => 1,
    handles => [qw( sysread )],
    trigger => sub { binmode +shift->{io}, ":bytes" },
    ;

我去年编写了一个大型测试类,它还使用 handles 功能将大量方法重新分派到底层 Selenium/WWWMech 对象。消失这种样板确实有助于提高可读性和维护性。

Use Moose. This is from something I wrote last night (using Mouse in this case). It should be pretty obvious what it does, what it validates, and what it sets up. Now imagine writing the equivalent raw OO. It's not super hard but it does start to get much harder to read and not just the code proper but the intent which can be the most important part when reading code you haven't seen before or in awhile.

has "io" =>
    is => "ro",
    isa => "FileHandle",
    required => 1,
    handles => [qw( sysread )],
    trigger => sub { binmode +shift->{io}, ":bytes" },
    ;

I wrote a big test class last year that also used the handles functionality to redispatch a ton of methods to the underlying Selenium/WWWMech object. Disappearing this sort of boilerplate can really help readability and maintenance.

无悔心 2024-11-23 12:13:34

我从未使用过 Moose,但使用过 Catalyst,并且在 OO Perl 和非 OO Perl 方面拥有丰富的经验。我的经验告诉我,最好的方法就是你最舒服的方法。

对我来说,这种方法已经成为“除了 Catalyst 之外的任何方法”:)(这并不是说那些热爱并信奉 Catalyst 的人是错误的——这只是我的口味)。

如果您已经拥有可以构建的爬虫应用程序的核心,请使用它所编写的任何内容。如果您从头开始,请使用您最有经验的内容 - 除非这是您扩展并尝试的机会一些新的东西,那么无论如何,在学习新东西的同时完成你的任务。

我认为这只是“哪种语言最好?”的另一个例子。除了个人之外,这个问题永远无法回答。

I've never used Moose, but I've used Catalyst, and have extensive experience with OO Perl and non-OO Perl. And my experience tells me that the best method to use is the method you're most comfortable using.

For me, that method has become "anything except Catalyst" :) (That's not to say that people who love and swear by Catalyst are wrong--it's just my taste).

If you already have the core of a crawler app that you can build on, use whatever it's written in. If you're starting from scratch, use whatever you have the most experience in--unless this is your chance to branch out and try something new, then by all means, accomplish your task while learning something new at the same time.

I think this is just another instance of "which language is best?" which can never be answered, except by the individual.

十年不长 2024-11-23 12:13:34

尽管这是 10 年前提出的问题,但 Perl 世界已经发生了很大变化。我想大多数人现在都发现 Moose 并没有实现人们所想象的一切。有几个衍生项目、大量 MooseX 垫片等等。如此多的流失是一些接近有用但不完全有用的代码的味道。甚至原作者 Stevan Little 也在研究它的替代品 Moxie,但它从未真正走向任何地方。我认为没有人对 Moose 完全满意。

目前,无论您对该框架的估计如何,未来可能不是 Moose。我在 Intermediate Perl 中有一个关于 Moose 的章节,但我会在下一版中删除它。这不是质量决定,只是该领域正在发生的事情的实际情况。

Ovid 正在开发 Corrina,这是一个类似但又不同的框架,试图解决一些相同的问题问题。 Ovid 概述了他想要改进的各种内容,因此您可以将此作为您自己决定的基础。无论您对 Moose 或 Corrina 有何看法,我认为社区现在正在摆脱 Moose 阶段。

您可能想听一下 Moose 如何让我成为一名糟糕的 OO 程序员,来自 Tadeusz Sośnierz 的简短演讲,地址: PerlCon 2019。

即使您持怀疑态度,我也会说在您承诺重新配置大型代码库之前在一个小项目中尝试 Moose。然后,在一个中等项目中尝试一下。我倾向于认为像 Moose 这样的框架在适合页面的各种示例中看起来很有吸引力,但直到问题变得更加复杂时才显示出它们的弱点。在您了解更多之前不要全力以赴。

不过,您至少应该了解 Moose 的基础知识,因为您可能会遇到使用它的其他代码。

Perl 5 Porters 甚至可能将这个包含在核心 Perl 中,但是完整的交付可能会在五到十年的范围内发生,即便如此,您也必须坚持使用受支持的 Perl。我遇到的大多数人在 Perl 版本上都落后了大约三到五年。如果你控制你的 perl 版本,那不是问题。但并不是每个人都这样做。坚持等待科琳娜可能不是一个好的短期计划。

Although this was asked 10 years ago, much has changed in the Perl world. I think most people now see that Moose didn't deliver all people thought it might. There are several derivative projects, lots of MooseX shims, and so on. That much churn is the code smell of something that's close to useful but not quite there. Even Stevan Little, the original author, was working on it's replacement, Moxie, but it never really went anywhere. I don't think anyone was ever completely satisfied with Moose.

Currently, the future is probably not Moose, irrespective of your estimation of that framework. I have a chapter for Moose in Intermediate Perl, but I'd remove it in the next edition. That's not a decision from quality, just on-the-ground truth from what's happening in that space.

Ovid is working on Corrina, a similar but also different framework that tries to solve some of the same problems. Ovid outlines various things he wants to improve, so you can use that as a basis for your own decision. No matter what you think about either Moose or Corrina, I think the community is now transitioning out of its Moose phase.

You may want to listen to How Moose made me a bad OO programmer, a short talk from Tadeusz Sośnierz at PerlCon 2019.

Even if your skeptical, I'd say try Moose in a small project before you commit to reconfiguring a large codebase. Then, try it in a medium project. I tend to think that frameworks like Moose look appealing in the sorts of examples that fit on a page, but don't show their weaknesses until the problems get much more complex. Don't go all in until you know a little more.

You should at least know the basics of Moose, though, because you are likely to run into other code that uses it.

The Perl 5 Porters may even include this one in core perl, but a full delivery may happen in the five to ten year range, and even then you'd have to insist on a supported Perl. Most people I encounter are about three to five years behind on Perl versions. If you control your perl version, that's not a problem. Not everyone does though. Holding out for Corrina may not be a good short-term plan.

花开浅夏 2024-11-23 12:13:34

当我了解 Perl 中的对象时,我首先想到的是为什么 Perl 通常试图使事情变得简单,但它却如此复杂。

通过 Moose,我发现 Perl 中可以实现简单的 OOP。它在某种程度上使 Perl 的 OOP 回到了可管理的水平。

(是的,我承认,我不喜欢 Perl 的对象设计。)

When I learned about objects in Perl, first thing I thought was why it's so complicated when Perl is usually trying to keep things simple.

With Moose I see that uncomplicated OOP is possible in Perl. It sort of bring OOP of Perl back to manageable level.

(yes, I admit, I don't like perl's object design.)

空城仅有旧梦在 2024-11-23 12:13:33

虽然我不同意 Flimzy 的介绍(“我没有用过 Moose,但是我用过这个使用 Moose 的东西”),但我同意他的前提。

使用您认为可以产生最佳结果的东西。如果(或一个)目标是学习如何有效地使用 Moose,那么使用 Moose。如果目标是编写好的代码,并且学习 Moose 会分散注意力,那么就不要使用 Moose。

然而,你的问题是开放式的(正如其他人指出的那样)。没有一个答案会被普遍接受为正确的,否则 Moose 的采用率会高得多,我也不会回答这个问题。我真的只能解释为什么我每次开始一个新项目时都选择使用 Moose。

正如 Sid 从 Moose 文档中引用的那样。 Moose 的核心目标是成为一种更清晰、标准化的方式来完成自 Perl 5.0 发布以来面向对象 Perl 程序员一直在做的事情。它提供了使做正确的事情比做错误的事情更简单的捷径。在我看来,这是标准 Perl 所缺乏的东西。它提供了新的工具,使您的问题更简单地抽象为更小、更容易解决的问题,并且它提供了强大的自省和元编程 API,试图规范化从 Perl 空间侵入 Perl 内部的野兽(即我以前提到的)作为符号表巫术)。

我发现自从我开始使用 Moose[^1] 以来,我对代码量“太多”的自然感觉已经减少了 66%。我发现我更容易遵循良好的设计原则,例如封装和信息隐藏,因为 Moose 提供的工具可以让这一切变得更容易。因为 Moose 自动生成了我通常必须写出的大部分样板文件(例如访问器方法、委托方法和其他此类内容),所以我发现可以更轻松地快速了解我六个月所做的事情前。我还发现自己编写的代码比几年前要少得多,只是为了节省自己敲击几次键盘的次数。

可以编写不使用 Moose[^2] 的干净、健壮、优雅的面向对象 Perl。根据我的经验,这需要更多的努力和自我控制。我发现,在项目要求我不能使用 Moose 的情况下,我的常规面向对象代码受益于我从 Moose 学到的习惯。我的思考方式与我考虑使用 Moose 编写它的方式相同,然后输入三倍于我写下我期望 Moose 为我生成的代码的代码[^3]。

所以我使用 Moose,因为我发现它让我成为更好的程序员,并且因为它我写出了更好的程序。如果您不认为这也适合您,那么 Moose 就不是正确的答案。

[^1]:当模块中的代码达到约 300 行时,我常常开始考虑我的设计。现在我开始对大约 100 行感到不安。

[^2]:Miyakawa 在 Twiggy 中的代码是我今天刚刚读到的一个很好的例子。

[^3]:这并不普遍正确。有很多关于人们通过过度使用 Moose 提供的工具编写出难以维护、可怕的代码的故事。糟糕的程序员可以在任何地方编写糟糕的代码。

While I disagree with Flimzy's introduction ("I've not used Moose, but I have used this thing that uses Moose"), I agree with his premise.

Use what you feel you can produce the best results with. If the (or a) goal is to learn how to effectively use Moose then use Moose. If the goal is to produce good code and learning Moose would be a distraction to that, then don't use Moose.

Your question however is open-ended (as others have pointed out). There is no answer that will be universally accepted as true, otherwise Moose's adoption rate would be much higher and I wouldn't be answering this. I can really only explain why I choose to use Moose every time I start a new project.

As Sid quotes from the Moose documentation. Moose's core goal is to be a cleaner, standardized way of doing what Object Oriented Perl programmers have been doing since Perl 5.0 was released. It provides shortcuts to make doing the right thing simpler than doing the wrong thing. Something that is, in my opinion, lacking in standard Perl. It provides new tools to make abstracting your problem into smaller more easily solved problems simpler, and it provides a robust introspection and meta-programming API that tries to normalize the beastiary that is hacking Perl internals from Perl space (ie what I used to refer to as Symbol Table Witchery).

I've found that my natural sense of how much code is "too much" has been reduced by 66% since I started using Moose[^1]. I've found that I more easily follow good design principles such as encapsulation and information hiding, since Moose provides tools to make it easier than not. Because Moose automatically generates much of the boiler-plate that I normally would have to write out (such as accessor methods, delegate methods, and other such things) I find that it's easier to quickly get up to speed on what I was doing six months ago. I also find myself writing far less tricky code just to save myself a few keystrokes than I would have a few years ago.

It is possible to write clean, robust, elegant Object Oriented Perl that doesn't use Moose[^2]. In my experience it requires more effort and self control. I've found that in those occasions where the project demands I can't use Moose, my regular Object Oriented code has benefitted from the habits I have picked up from Moose. I think about it the same way I would think about writing it with Moose and then type three times as much code as I write down what I expect Moose would generate for me[^3].

So I use Moose because I find it makes me better programmer, and because of it I write better programs. If you don't find this to be true for you too, then Moose isn't the right answer.

[^1]: I used to start to think about my design when I reached ~300 lines of code in a module. Now I start getting uneasy at ~100 lines.

[^2]: Miyagawa's code in Twiggy is an excellent example that I was reading just today.

[^3]: This isn't universally true. There are several stories going around about people who write less maintainable, horrific code by going overboard with the tools that Moose provides. Bad programmers can write bad code anywhere.

陌伤ぢ 2024-11-23 12:13:33

您可以在 Moose 的文档中找到为什么使用 Moose 的答案。

Moose 的主要目标是使 Perl 5 面向对象编程更容易、更一致、更不乏味。使用 Moose,您可以更多地考虑您想要做什么,而不是考虑 OOP 的机制。

根据我的经验,其他人可能也会告诉你同样的事情。 Moose 极大地减少了您的代码大小,它具有很多功能,并且只有标准功能,例如验证、创建对象时强制值、惰性验证、默认值等,这些功能非常简单易读,您将永远不想错过 Moose。

You find the answer why to use Moose in the Documentation of it.

The main goal of Moose is to make Perl 5 Object Oriented programming easier, more consistent, and less tedious. With Moose you can think more about what you want to do and less about the mechanics of OOP.

From my experience and probably others will tell you the same. Moose reduces your code size extremly, it has a lot of features and just standard features like validation, forcing values on creation of a object, lazy validation, default values etc. are just so easy and readable that you will never want to miss Moose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文