Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl Moops和默认值_Perl_Oop_Moose_Moops - Fatal编程技术网

Perl Moops和默认值

Perl Moops和默认值,perl,oop,moose,moops,Perl,Oop,Moose,Moops,我试图了解lexical\u的属性如何在中工作。根据我的理解,lexical\u has函数能够通过使用标量引用(保存在访问器=>中)生成code对类可能“在词汇上拥有”的任何属性的引用。然后可以使用代码引用以“强制”作用域的方式访问class属性(因为它们是“由内而外”?)。但这只是我的猜测和胡乱猜测,所以我希望有更好的解释。我还想知道为什么这种方法在以下示例中不起作用: 我正在创建一辆类汽车: use Moops; class Car { lexical_has max_speed

我试图了解
lexical\u的属性如何在中工作。根据我的理解,
lexical\u has
函数能够通过使用标量引用(保存在
访问器=>
中)生成
code
可能“在词汇上拥有”的任何属性的引用。然后可以使用代码引用以“强制”作用域的方式访问class属性(因为它们是“由内而外”?)。但这只是我的猜测和胡乱猜测,所以我希望有更好的解释。我还想知道为什么这种方法在以下示例中不起作用:

我正在创建一辆
类汽车

use Moops;

class Car {
    lexical_has max_speed => (
        is       => 'rw',
        isa      => Int,
        default  => 90,
        accessor => \(my $max_speed),
        lazy     => 1,    
    );

    has fuel => (
        is  => 'rw',
        isa => Int,
    );

    has speed => (   
        is       => 'rw',
        isa      => Int,
        trigger  => method ($new, $old?) {
            confess "Cannot travel at a speed of $new; too fast"
                if $new > $self->$max_speed;
        },
    );

    method get_top_speed() {
        return $self->$max_speed;  
    }    
}     
然后我实例化该对象并尝试使用其方法访问其属性:

my $solarcharged = Car->new ;

# This correctly won't compile due to $max_speed scoping:
# say $solarcharged->$max_speed;

# This shows expected error "too fast"
$solarcharged->speed(140);

# This prints nothing - wrong behavior?
say $solarcharged->get_top_speed();
使用自定义访问器的最后一行让我感到困惑:什么都没有发生。我是否缺少类的属性或设置(将其标记为“渴望”或“懒惰”=>0
不起作用)?我是否需要
构建
功能?是否缺少初始化步骤

N.B.如果我向类添加一个setter方法,如下所示:

method set_top_speed (Int $num) {   
    $self->$max_speed($num);
}
然后在我的最后一系列陈述中称之为:

# shows expected error "too fast"
$solarcharged->speed(140);

$solarcharged->set_top_speed(100);

# prints 100
say $solarcharged->get_top_speed();
get\u top\u speed()
方法开始正确返回。这是预期的吗?如果是,那么类设置的默认值是如何工作的


我已将此报告为错误:


由于可以通过使用“perl约定”(即不使用
lexical\u has
并在私有属性前面加上“
\u
”)轻松解决这个问题,并且这个问题是由一个bug引起的,因此我不希望得到修复或补丁。对于赏金-我希望能解释一下
Lexical::Accessor
应该如何工作;它如何“强制”访问者的私有内部作用域;也许是一些CS理论解释了为什么这是一件好事。

根据OP提交的文件,这个错误在词法存取器0.009中得到了修复。

最小的测试用例似乎是
包a{use Moo;use Lexical::Accessor;Lexical_has attr=>(Accessor=>\my$attr,default=>sub{90});sub get{shift()->$attr};说A->new->get()/“undef”
(打印
undef
,而不是默认值)。这意味着它不依赖于
Moops
。这似乎是一个真正的错误;你报告了吗?这是一个错误。我相信它会影响读写访问器,但不会影响读写器。我现在还没有机会找出原因,但希望在接下来的几天内。PS:这可能是在我最近对词法::访问器的重构中引入的。作为一种解决方法,您可以尝试降级到0.003。快速介绍由内而外属性的概念,但不适用于驼鹿环境: