perl - 在 Moose 中创建一个 Web::Scraper 对象作为类属性

标签 perl moose

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

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

但是当我在我的类(class)中放置相同的代码时,我收到以下错误:
Not a HASH reference at accessor MyModule::scraper

这是我的代码:
has 'scraper' => (is => 'rw', isa => 'Web::Scraper',  default => sub { scraper( sub { } ); });

以下是 Moose 的更多错误:
# 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
# 

最佳答案

您正遭受 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 { });
    },
);

关于perl - 在 Moose 中创建一个 Web::Scraper 对象作为类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967068/

相关文章:

perl - 覆盖类的 "is"属性(Moose)

perl - 用于 perl 的 ruby httparty?

perl dbi : fetchrow_arrayref

performance - 检查 Perl 函数参数值得吗?

perl - Unblessing Perl 对象并为 convert_blessed 构造 TO_JSON 方法

arrays - Perl Moose 对象可以直接具有数组/哈希属性吗?

Perl OO 框架和程序设计 - Moose 和 Conway 的由内而外的对象 (Class::Std)

regex - Perl 正则表达式图灵完备吗?

perl - 如何从Perl的DBI中获取模式?

arrays - Perl @$ 语法含义