如何Perl的方法,属性工作? [英] How do Perl method attributes work?

查看:114
本文介绍了如何Perl的方法,属性工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个鲜为人知内置Perl的功能属性。然而,官方文档正在做一个相当糟糕的工作介绍给新手的概念。与此同时,像催化剂使用广泛的框架属性,这似乎使许多事情变得更容易出现。由于使用的东西不知道影响吮吸了一下,我想知道的细节。语法明智的,他们看起来像Python的装饰,但文档意味着简单的东西。

A little known built-in Perl feature is attributes. However, the official documentation is doing a rather bad job introducing newbies to the concept. At the same time, frameworks like Catalyst use attributes extensively which seems to make many things easier there. Since using something without knowing the implications sucks a bit, I'd like to know the details. Syntax-wise they look like Python's decorators, but the documentation implies something simpler.

你能解释(与真实世界的例子如果可能的话)是什么属性好,什么发生在门后?

Could you explain (with real-world examples if possible) what attributes are good for and what happens behind the doors?

推荐答案

您是对的,文档是不是在这方面很清楚,尤其是因为属性不是那么复杂。如果定义了一个子程序的属性,如:

You are right, the documentation is not very clear in this area, especially since attributes are not so complicated. If you define a subroutine attribute, like this:

sub some_method :Foo { }

Perl会的在编译程序的(这很重要)寻找神奇的子 MODIFY_ code_ATTRIBUTES 在当前包或任何它的父类。这将与当前包,到您的子程序的参考,以及用于该子程序中定义的属性的列表的名称被调用。如果该处理程序不存在,编译将失败。

Perl will while compiling your program (this is important) look for the magic sub MODIFY_CODE_ATTRIBUTES in the current package or any of its parent classes. This will be called with the name of the current package, a reference to your subroutine, and a list of the attributes defined for this subroutine. If this handler does not exist, compilation will fail.

在此处理程序做什么完全取决于你。恩,那就对了。没有隐藏任何魔法。如果你想发出一个错误,退回违规属性的名称会导致编译失败与无效属性消息。

What you do in this handler is entirely up to you. Yes, that's right. No hidden magic whatsoever. If you want to signal an error, returning the name of the offending attributes will cause the compilation to fail with an "invalid attribute" message.

有一个名为另一个处理程序 FETCH_ code_ATTRIBUTES 每当有人说,会叫

There is another handler called FETCH_CODE_ATTRIBUTES that will be called whenever someone says

use attributes;
my @attrs = attributes::get(\&some_method);

此处理程序获取通过包名和子程序参考,并应该返回子程序的属性的列表(虽然你真的什么又是你的)。

This handler gets passed the package name and subroutine reference, and is supposed to return a list of the subroutine's attributes (though what you really do is again up to you).

下面是使简单的任意属性,您可以在以后的查询方法标签的例子:

Here is an example to enable simple "tagging" of methods with arbitrary attributes, which you can query later:

package MyClass;
use Scalar::Util qw( refaddr );

my %attrs; # package variable to store attribute lists by coderef address

sub MODIFY_CODE_ATTRIBUTES {
    my ($package, $subref, @attrs) = @_;
    $attrs{ refaddr $subref } = \@attrs;
    return;
}

sub FETCH_CODE_ATTRIBUTES {
    my ($package, $subref) = @_;
    my $attrs = $attrs{ refaddr $subref };
    return @$attrs;
}

1;

现在,MyClass中及其所有子类,你可以随心所欲的使用属性,并利用它们进行查询属性::得到()

Now, in MyClass and all its subclasses, you can use arbitrary attributes, and query them using attributes::get():

package SomeClass;
use base 'MyClass';
use attributes;

# set attributes
sub hello :Foo :Bar { }

# query attributes
print "hello() in SomeClass has attributes: ",
      join ', ', attributes::get(SomeClass->can('hello'));

1;
__END__
hello() in SomeClass has attributes: Foo, Bar

总之,属性不会做很多这从另一方面使得它们非常灵活:可以使用他们作为真正的属性(如本例所示),实施类似的装饰(见<一href=\"http://stackoverflow.com/questions/987059/how-do-perl-method-attributes-work/987216#987216\">Sinan's回答),或者为自己的不正当的目的。

In summary, attributes don't do very much which on the other hand makes them very flexible: You can use them as real "attributes" (as shown in this example), implement something like decorators (see Sinan's answer), or for your own devious purposes.

这篇关于如何Perl的方法,属性工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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