Creating a Web :: Scraper Object as a Class Attribute in Moose

I am trying to imitate the code found here, specifically, this attribute:

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

But when I put the same code in my class, I get the following error:

Not a HASH reference at accessor MyModule::scraper

Here is my code:

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

Here are more errors from 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
# 
+4
source share
1 answer

You suffer from a name clash between a function scraperimported from Web::Scraperand the access method scraperdefined has 'scraper' => .... This is what the warning means You are overwriting a locally defined function (scraper) with an accessor.

, scraper( sub { } ); accessor , , , , 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 { });
    },
);
+5

Source: https://habr.com/ru/post/1668446/


All Articles