English 中文(简体)
我怎么能读一下摩西物体的属性?
原标题:How can I acces read-only attributes of Moose objects?
  • 时间:2010-10-01 13:39:01
  •  标签:
  • perl
  • moose

I m a fundamental a final newbie : ± >; ± >; 远距I have Reada href= <> >nolow >%20code ; ±>; α->; α->; α- href="; .org/Mo%3%C>a>>> > > > > >:pfollow >; α->; α->; >; >; >; 。

我拿不到几个东西。 我制定了以下一揽子计划:

package MyRange;

use Moose;
use namespace::autoclean;

has [  start ,  end  ] => (
    is       =>  ro ,
    isa      =>  Int ,
    required => 1,
);

__PACKAGE__->meta->make_immutable;

1;

然后:

use MyRange;    
my $br = MyRange->new(
    start                => 100,
    end                  => 180
);

现在,我可以通过如下途径进入我的田地:$br->{},但我也可以通过$br->{start}=5000加以修改(尽管这些只是“阅读”)。 我还可以添加新的钥匙,如<代码> 日记和编号;{xxx}=111。

我没有什么东西? 物体是否受到某种保护? <代码>/>的含义是什么?

最佳回答

当你说<条码>is =>ro 时,你告诉Moose为你创建只读取器,即一种读取方法。 你呼吁

$br->start;

$br->end;

采用这些方法确定属性将形成一个例外:

$br->start(42);

If you had used is => rw , then the above would w或k and update the attribute s value.

你重新做的是直接接触该物体,这违反了cap头,在使用摩苏尔时也没有必要。

The Moose manual, i.e. all the documents under the Moose::Manual namespace explain that in detail. A good starting point f或 questions like this is probably Moose::Manual::Attributes.

问题回答

当你用<条码>日记和>;{>>>>>>><>条/代码”获取属性时,你正在绕过入口,而且你正在直接支持基本内容的执行。 你可以这样做,但你不应该这样做。 而且,如果修改执行,你的法典将打破。

否则,你就应当使用“接触者”方法获取属性:

my $start = $br->start;

当你说属性为RO时,这意味着不允许你使用用户改变属性价值:

$br->start(32);




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签