Search code examples
perlmoose

Creating a Web::Scraper object as a class attribute in Moose


I'm trying to mimic 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's my code:

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

Here is more of the error 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
# 

Solution

  • You're suffering from a name clash between the scraper function imported from Web::Scraper and the scraper accessor method defined by has 'scraper' => .... This is what the warning You are overwriting a locally defined function (scraper) with an accessor is referring to.

    The way your code is currently written, your default initializer scraper( sub { } ); ends up calling the accessor method directly, which promptly dies because it expects an object as its first argument, not sub { }.

    The easiest way to work around the problem is to rename your attribute. Unfortunately other ways are harder because Web::Scraper doesn't support renaming or suppressing its exported symbols.

    The following code is untested, but it might work if you want to keep the name of your attribute the same:

    use Moose;
    use Web::Scraper;
    BEGIN { *_web_scraper = \&scraper; undef &scraper; }
    
    has 'scraper' => (
        is      => 'ro',
        isa     => 'Web::Scraper',
        default => sub {
            _web_scraper(sub { });
        },
    );