Perl Moose属性不强制类型检查 [英] Perl Moose attribute not force type checking

查看:127
本文介绍了Perl Moose属性不强制类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用草莓Perl, 驼鹿2.0010

I an using strawberry perl, Moose 2.0010

在课堂上:

package Cat;
use 5.010;
use strict;
use Moose;

has 'name',       is => 'ro', isa => 'Str', default => 'Beauty';
#has 'age',       is => 'ro';
has 'diet',       is => 'rw', default => 'fish';
has 'birth_year', is => 'ro', isa=> 'Int',
                  default => 1997;

在申请中:

use 5.010;
use strict;
use Cat;

my $kitty = Cat->new(name => 123, diet => 'Sea food', 
                     birth_year => 'nineteen ninety seven');
say 'I have a kitten named ', $kitty->name(), ' eats ', $kitty->diet(),
    ' birth at ', $kitty->birth_year();

输出:

I have a kitten named 123 eats Sea food birth at nineteen ninety seven
Press any key to continue . . .

它不会强制类型检查.

完整的代码,其余代码由Padre生成,我没有删除它. Padre添加了尾随1;:

The complete code, the rest of code is generated by Padre, I haven't delete it. Padre added trailing 1;:

package Cat;
use 5.010;
use strict;
use Moose;

has 'name',       is => 'ro', isa => 'Str', default => 'Beauty';
#has 'age',       is => 'ro';
has 'diet',       is => 'rw', default => 'fish';
has 'birth_year', is => 'ro', isa=> 'Int',
                  default => 1997;
sub age
{
    my $self = shift;
    my $year = (localtime)[5] + 1900;

    return $year - $self->birth_year();
}

=pod

=head1 NAME

Cat - My author was too lazy to write an abstract

=head1 SYNOPSIS

  my $object = Cat->new(
      foo  => 'bar',
      flag => 1,
  );

  $object->dummy;

=head1 DESCRIPTION

The author was too lazy to write a description.

=head1 METHODS

=cut

use 5.006;
use strict;
use warnings;

our $VERSION = '0.01';

=pod

=head2 new

  my $object = Cat->new(
      foo => 'bar',
  );

The C<new> constructor lets you create a new B<Cat> object.

So no big surprises there...

Returns a new B<Cat> or dies on error.

=cut

sub new {
    my $class = shift;
    my $self  = bless { @_ }, $class;
    return $self;
}

=pod

=head2 dummy

This method does something... apparently.

=cut

sub dummy {
    my $self = shift;

    # Do something here

    return 1;
}

1;

=pod

=head1 SUPPORT

No support is available

=head1 AUTHOR

Copyright 2011 Anonymous.

=cut

推荐答案

问题是在Cat.pm的第64行定义的new. Moose提供了new方法,因此您无需编写自己的方法.删除该new方法,它会正常工作.

The problem is the new defined at line 64 of Cat.pm. Moose provides a new method, so you don't need to write your own. Remove that new method, and it works fine.

应该触发啊哈!"的另一位是

The other bit that should have triggered a "aha!" is the

use 5.006;
use strict;
use warnings;

显示在您的Cat.pm代码的一半位置.

which is present halfway down your Cat.pm code.

如果要使用POD文档,则应将代码嵌入文档中(允许文档增强内联注释);或者您应该将所有代码放在顶部,并在底部放置一个清晰的POD.无论哪种方式,一致性都将有助于避免将来出现此类问题.

If you're going to use POD documentation, you should either embed your code within the docs (allowing the docs to enhance your inline comments); or you should put all your code up top and make a clear POD at the bottom. Either way, consistency will help avoid these kinds of problems in the future.

这篇关于Perl Moose属性不强制类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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