是否可以在不定义 getter 的情况下访问 Perl 包的 ATTR? [英] Is possible to access ATTR for Perl packages without defining a getter?

查看:45
本文介绍了是否可以在不定义 getter 的情况下访问 Perl 包的 ATTR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事 Perl 5.8 项目.我的包看起来像:

I am currently working on a Perl 5.8 project. I have package that looks like:

package Foo::Bar;

use strict;
use warnings;
use Class::Std;

our @EXPORT_SAFE = qw(Class::Std);

my %baz :ATTR;

sub BUILD {
  my ($self, $ident, $args) = @_;
  
  $baz{$ident} = $$args{something};
}

我没有将 baz 暴露给任何 getter,但我想获取它的内容以进行单元测试.在 Perl 中有什么方法可以做到吗?

I am not exposing baz with any getter, but I would like to fetch it's content for unit testing purposes. There is any way to do it in Perl?

例如在 Ruby 中,您可以 some_instance.instance_variable_get("@#{name}")

e.g. in Ruby you can some_instance.instance_variable_get("@#{name}")

在此先非常感谢您

推荐答案

TL;DR

一般情况下的答案是这取决于(但可能)".

The answer in the general case is "it depends (but probably)".

您的情况的答案是否".

The answer in your case is "no".

细节

在 Perl 中没有固定的方法来实现类和对象.在大多数情况下,对象将是对哈希的祝福引用.在这些情况下,您可以简单地将对象引用视为散列引用并直接查找键.所以,代码如下:

There is no fixed way to implement classes and objects in Perl. In most cases an object will be a blessed reference to a hash. In those cases, you could simply treat the object reference as a hash reference and look up the key directly. So, code like this:

my $baz = $obj->{baz};

但是,您没有使用最常见的方法来构建 Perl 对象,而是使用了 类::标准.而 Class::Std 的处理方式则大不相同.Class::Std 使用一种称为由内向外对象"的方法;它由 Damian Conway 在 Object Oriented Perl 中普及,大约在二十年前非常流行.

However, you're not using the most common approach to building Perl objects, you're using Class::Std. And Class::Std does things rather differently. Class::Std uses an approach called "inside-out objects" which was popularised by Damian Conway in Object Oriented Perl and which was very fashionable for a while about twenty years ago.

在普通对象中,每个对象都有一个哈希值.键是属性名称,值是属性值.对于由内而外的对象,每个属性都有自己的哈希值.键是对单个对象的字符串化引用,值是该对象的属性值.

In normal objects, you have a hash for each object. The keys are the attribute names and the values are the attribute values. With inside-out objects, each attribute has its own hash. The keys are the stringified references to an individual object and the value is the attribute's value for that object.

实际上,标准"方法是:

In effect, the "standard" approach is:

$object{attr} = value;

由内而外的方法是:

$attr{object} = value;

人们喜欢由内而外的对象的一个​​重要原因是属性哈希可以是存储在类源代码中的词法变量.这意味着它们是真正私有的,不能从类外部访问 - 除非使用提供的访问器方法.如果您想强制 Perl 实现一种更强大的封装,这很好.但是如果你想像这里那样突破这种封装,那就太糟糕了.

One of the big reasons why people liked inside-out objects is that the attribute hashes can be lexical variables stored in the class's source code. And that means they are truly private and cannot be accessed from outside of the class - except by using the provided accessor methods. Which is great if you want to force Perl to implement a far stronger kind of encapsulation. But terrible if you want to break through that encapsulation in the way that you do here.

所以,不.就您而言,您不能这样做.

So, no. In your case, you can't do this.

这篇关于是否可以在不定义 getter 的情况下访问 Perl 包的 ATTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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