perl Moose 访问器疯狂 - 无法仅定义读取器或写入器访问器!

发布于 2024-09-27 15:59:39 字数 956 浏览 6 评论 0原文

所以我只是想做一件非常简单的事情:为驼鹿属性定义一个自定义读取器访问器。所以我尝试这样做:

has 'compiled_regex' => ( 
    isa => 'RegexpRef', 
    is => 'rw', 
    reader => 'get_compiled', 
);

但是 get_compiled 永远不会被调用,大概是因为 compiled_regex 是读/写的。好的,没问题。我接下来尝试这个:

 has 'compiled_regex' => ( 
     isa => 'RegexpRef', 
     writer => '_compile', 
     reader => 'get_compiled', 
 );

这会产生以下错误:

无法通过包“PrettyRegex”在 ../lib/Pretty/Regexs.pm 第 39 行找到对象方法“compiled_regex”。

它引用 _compile 方法中的这一行:

$self->compiled_regex(qr/$self->regex/);

现在我在过去的三天里没有睡太多觉,所以也许我很困惑,但似乎即使这确实有效,它也会造成无限的回归,因为我将编写器定义为 _compile ...那么我在这里缺少什么?

尝试了思南的回答,但仍然得到:

Can't locate object method "compiled_regex" via package "PrettyRegex" at ../lib/Pretty/Regexs.pm line 41.

So I'm just trying to do a very simple thing: define a custom reader accessor for a moose attribute. So I try this:

has 'compiled_regex' => ( 
    isa => 'RegexpRef', 
    is => 'rw', 
    reader => 'get_compiled', 
);

but get_compiled never gets called, presumably because compiled_regex is read/write. Ok, no problem. I next try this:

 has 'compiled_regex' => ( 
     isa => 'RegexpRef', 
     writer => '_compile', 
     reader => 'get_compiled', 
 );

and that produces the following error:

Can't locate object method "compiled_regex" via package "PrettyRegex" at ../lib/Pretty/Regexs.pm line 39.

which refers to this line which is in the _compile method:

$self->compiled_regex(qr/$self->regex/);

Now I haven't gotten much sleep in the past 3 days so maybe I'm confused, but it seems that even if this did work, it would create an infinite regress since I've defined the writer as _compile ... so what am I missing here?

tried Sinan answer but still getting:

Can't locate object method "compiled_regex" via package "PrettyRegex" at ../lib/Pretty/Regexs.pm line 41.

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

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

发布评论

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

评论(3

短叹 2024-10-04 15:59:39

我不清楚你想做什么。 readerwriter 是 Moose 为您创建的方法,而不是您编写并由 Moose 调用的方法。

我认为您需要重申您的问题以解释您想要解决的更高级别的问题。我希望有比您目前想到的更好的方法来做到这一点,但如果不知道您真正想要做什么,我们就无法确定。

如果您尝试在读取属性时调用自定义方法,只需为读取器命名其他名称(例如 _get_compiled_regex),并将您的方法命名为 compiled_regex。或者在 reader 方法上使用方法修饰符。 (这可能更好,因为如果有人将参数传递给您的阅读器方法并尝试设置属性,您就不会忘记die。)

您可能还希望在其他属性上有一个触发器这清除了这一点。

I'm unclear on what you're trying to do. reader and writer are methods that Moose creates for you, not methods that you write and it calls.

I think you need to restate your question to explain the higher-level problem that you're trying to solve. I expect there's a better way to do it than you've currently thought of, but we can't be sure without knowing what you're really trying to do.

If you're trying to get your custom method called when the attribute is read, just name the reader something else (like _get_compiled_regex), and name your method compiled_regex. Or use a method modifier on the reader method. (That's probably better, because then you won't forget to die if somebody passes a parameter to your reader method, trying to set the attribute.)

You also might want to have a trigger on some other attribute that clears this one.

千年*琉璃梦 2024-10-04 15:59:39

我一直在猜测实际的问题是什么,但我有一种感觉:

package My::M;

use Moose;
use namespace::autoclean;

has 'compiled_regex' => (
    isa => 'RegexpRef',
    is  => 'ro',
    writer => '_set_compiled_regex',
);

sub compile {
    my $self = shift;
    my ($pat) = @_;
    $self->_set_compiled_regex(qr/$pat/);
    return;
}

__PACKAGE__->meta->make_immutable;

package main;
use strict; use warnings;

my $m = My::M->new;
$m->compile( '^\W+\z' );

if ( '@#$%%
 =~ $m->compiled_regex ) {
    print "Hmph!\n";
}

I keep guessing at what the actual question is, but I have a feeling the following corresponds to it:

package My::M;

use Moose;
use namespace::autoclean;

has 'compiled_regex' => (
    isa => 'RegexpRef',
    is  => 'ro',
    writer => '_set_compiled_regex',
);

sub compile {
    my $self = shift;
    my ($pat) = @_;
    $self->_set_compiled_regex(qr/$pat/);
    return;
}

__PACKAGE__->meta->make_immutable;

package main;
use strict; use warnings;

my $m = My::M->new;
$m->compile( '^\W+\z' );

if ( '@#$%%
 =~ $m->compiled_regex ) {
    print "Hmph!\n";
}
不可一世的女人 2024-10-04 15:59:39

呃?如果您的阅读器名为 get_compiled,而您的编写器名为 _compile,那么您没有名为 compiled_regex 的方法,它应该是显而易见,为什么调用那个不存在的方法会失败。你需要后退几步并解释你想要做什么,而不是解释你尝试做的方式出了什么问题。

Er? If your reader is called get_compiled, and your writer is called _compile, then you don't have a method named compiled_regex, and it should be obvious why a call to that nonexistent method would fail. You need to take several steps back and explain what you're trying to do, instead of what's going wrong with the way you've tried to do it.

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