如何在面向对象的Perl中定义私有或内部方法? [英] How do I define private or internal methods in object oriented Perl?

查看:82
本文介绍了如何在面向对象的Perl中定义私有或内部方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用达米安·康威(Damian Conway)的由内而外"的对象,这是他的精彩著作 Perl最佳做法,以在我的客户端上构建到安全系统的面向对象的界面.我遇到了在我的模块中使用内部辅助方法的需求,我通常将其指定为"_some_method".但是,这似乎破坏了封装,因为可以通过包名称直接调用它们.有什么方法可以使这些方法真正私有?例如,

use SOD::MyOOInterface;

my $instance1 = SOD::MyOOInterface->new();
$instance1->_some_method;  #this produces an error: 
SOD::MyOOInterface::_some_method;   # this results in a 
                                    # successful method call 

很明显,我不希望_some_method的直接调用成功.有什么办法可以保证吗?

解决方案

不要将PBP用于对象实践.很老了实际上,现在可以在 Moose 中找到有关Perl和对象的最佳做法,是Perl的必备软件.

简而言之,Perl模糊名称空间和类的方式可以在类上静态调用大多数方法.这不是一件坏事,只是不要记录在案.确实没有理由将方法密封到实例中.没有私有方法会很烦人,但是不依赖未记录方法的惯例是如此强大,足以满足我们的社区需求.

特质实际上是可以在运行时编译为对象的角色(不允许实例化).这将进一步使典型用户无法理解方法的来源(因为它们将不在原始类中),但这要花运行时间. 有关性状的更多信息,请参见 MooseX :: Traits .

前面的下划线是一个很好的约定,可以进一步说明该方法专用于凝视的眼睛.

作为最后一个提示,如果您确实要解决此问题,则可以使用Class :: MOP :: Class-> create_anon_class()

使用这些方法创建一个匿名类

I'm using Damian Conway's "inside-out" objects as described is his wonderful book Perl Best Practices to construct an object-oriented interface to a security system at my client. I'm coming across the need to use internal helper methods within my module that I would normally designate as "_some_method". However, this seems to break encapsulation since they can be called directly via the package name. Is there any way of making these methods truly private? As an example,

use SOD::MyOOInterface;

my $instance1 = SOD::MyOOInterface->new();
$instance1->_some_method;  #this produces an error: 
SOD::MyOOInterface::_some_method;   # this results in a 
                                    # successful method call 

Obviously I don't want the direct call of _some_method to succeed. Is there any way of guaranteeing this?

解决方案

Don't use the PBP for object practices. It is very old. In fact, now the best practices regarding Perl and objects can be found in Moose, an almost must-have for Perl.

In short, the way Perl blurs namespaces and classes most methods can be called statically on the class. This is not a bad thing, just don't document it. There is really no reason to want to seal the methods into the instance. Not having private methods is kind of annoying but the convention of not relying on undocumented methods is so strong it has sufficed for our community.

A trait is effectively a role (doesn't permit instantiation) that can be compiled into an object at runtime. This will further obscure the origin of the methods from your typical user (because they won't be in the original class), but it comes at a runtime cost. See MooseX::Traits for more information on traits.

The prepending underscore is a great convention to further state the method is private to peering eyes.

As a last note if you really want to push this issue, you might be able to create an anonymous class with those methods using Class::MOP::Class->create_anon_class()

这篇关于如何在面向对象的Perl中定义私有或内部方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆