在Moose中创建Web :: Scraper对象作为类属性

时间:2017-01-31 20:39:33

标签: perl moose

我试图模仿code found here,具体,这个属性:

# web-scraper object
has 'worker' => (
    is      => 'ro',
    isa     => 'Web::Scraper',
    default => sub {
        scraper(sub { });
    }
);

但是当我在我的班级中输入相同的代码时,我收到以下错误:

Not a HASH reference at accessor MyModule::scraper

这是我的代码:

has 'scraper' => (is => 'rw', isa => 'Web::Scraper',  default => sub { scraper( sub { } ); });

以下是穆斯的错误:

# There were 1 warning(s)
#   Previous test 0 ''
#   You are overwriting a locally defined function (scraper) with an accessor at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 1047, <DATA> line 1.
#   Moose::Meta::Attribute::_process_accessors(Moose::Meta::Attribute=HASH(0x3b19838), "accessor", "scraper", undef) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Attribute.pm line 445
#   Class::MOP::Attribute::install_accessors(Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 994
#   Moose::Meta::Attribute::install_accessors(Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 897
#   Class::MOP::Class::try {...} () called at /usr/share/perl5/Try/Tiny.pm line 81
#   eval {...} called at /usr/share/perl5/Try/Tiny.pm line 72
#   Try::Tiny::try(CODE(0x3b076f0), Try::Tiny::Catch=REF(0x3b195e0)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 902
#   Class::MOP::Class::_post_add_attribute(Moose::Meta::Class=HASH(0x349f920), Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Mixin/HasAttributes.pm line 38
#   Class::MOP::Mixin::HasAttributes::add_attribute(Moose::Meta::Class=HASH(0x349f920), Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Class.pm line 573
#   Moose::Meta::Class::add_attribute(Moose::Meta::Class=HASH(0x349f920), "scraper", "definition_context", HASH(0x2921ce8), "is", "rw", "isa", "Web::Scraper", "default", ...) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose.pm line 76
#   Moose::has(Moose::Meta::Class=HASH(0x349f920), "scraper", "is", "rw", "isa", "Web::Scraper", "default", CODE(0x3ae1a60)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Exporter.pm line 408
#   Moose::has("scraper", "is", "rw", "isa", "Web::Scraper", "default", CODE(0x3ae1a60)) called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 33
#   require LaborData/DataFetcher.pm called at ./labor_data.t line 27
#   main::BEGIN() called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 0
#   eval {...} called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 0
#  at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 1047, <DATA> line 1.
#   Moose::Meta::Attribute::_process_accessors(Moose::Meta::Attribute=HASH(0x3b19838), "accessor", "scraper", undef) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Attribute.pm line 445
#   Class::MOP::Attribute::install_accessors(Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 994
#   Moose::Meta::Attribute::install_accessors(Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 897
#   Class::MOP::Class::try {...} () called at /usr/share/perl5/Try/Tiny.pm line 81
#   eval {...} called at /usr/share/perl5/Try/Tiny.pm line 72
#   Try::Tiny::try(CODE(0x3b076f0), Try::Tiny::Catch=REF(0x3b195e0)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 902
#   Class::MOP::Class::_post_add_attribute(Moose::Meta::Class=HASH(0x349f920), Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Mixin/HasAttributes.pm line 38
#   Class::MOP::Mixin::HasAttributes::add_attribute(Moose::Meta::Class=HASH(0x349f920), Moose::Meta::Attribute=HASH(0x3b19838)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Class.pm line 573
#   Moose::Meta::Class::add_attribute(Moose::Meta::Class=HASH(0x349f920), "scraper", "definition_context", HASH(0x2921ce8), "is", "rw", "isa", "Web::Scraper", "default", ...) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose.pm line 76
#   Moose::has(Moose::Meta::Class=HASH(0x349f920), "scraper", "is", "rw", "isa", "Web::Scraper", "default", CODE(0x3ae1a60)) called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Exporter.pm line 408
#   Moose::has("scraper", "is", "rw", "isa", "Web::Scraper", "default", CODE(0x3ae1a60)) called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 33
#   require LaborData/DataFetcher.pm called at ./labor_data.t line 27
#   main::BEGIN() called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 0
#   eval {...} called at /home/steve/perl/perl-lib/LaborData/DataFetcher.pm line 0
# 

1 个答案:

答案 0 :(得分:5)

您从scraper导入的Web::Scraper函数与scraper定义的has 'scraper' => ...访问者方法之间存在名称冲突。这就是警告You are overwriting a locally defined function (scraper) with an accessor所指的内容。

当前编写代码的方式,默认初始值设定项scraper( sub { } );最终会直接调用访问器方法,因为它期望将对象作为其第一个参数,而不是sub { }

解决此问题的最简单方法是重命名属性。不幸的是,其他方法更难,因为Web::Scraper不支持重命名或抑制其导出的符号。

以下代码未经测试,但如果您希望保持属性名称相同,则可能会有效:

use Moose;
use Web::Scraper;
BEGIN { *_web_scraper = \&scraper; undef &scraper; }

has 'scraper' => (
    is      => 'ro',
    isa     => 'Web::Scraper',
    default => sub {
        _web_scraper(sub { });
    },
);