moose

The type constraint 'XYZ' has already been created

被刻印的时光 ゝ 提交于 2019-12-11 04:55:40
问题 I want to use a Moose::Util::TypeConstraints in my application. So I define one in my main.pl main.pl use Moose::Util::TypeConstraints; subtype 'mySpecialType' => as 'Object' => where sub { $_->does('something') }; use noUse; In the package noUse.pm are packages used, which use the type constraint noUse.pm package noUse; use Use1; use Use2; 1; and my package Use1 and Use2 are working with the type constraint Use1.pm package Use1; use Moose; has 'object1' => ( is => 'ro', isa => 'mySpecialType

Moose structured types

我的梦境 提交于 2019-12-11 02:58:11
问题 I'd like to create a structured type in Moose that can be used as the type for another Moose attribute. For example, I'd like to be able to create a name attribute which has its own value and error attributes. I would therefore like to know the best way of achieving this. I have created a working example by defining a simple Moose class to represent a generic Field object. This has the value and error attributes. I have then created another Moose class for the Person object. This has id and

Perl Moose accessors generated on the fly

淺唱寂寞╮ 提交于 2019-12-11 01:47:32
问题 See the following fragment of Perl code which is based on Moose: $BusinessClass->meta->add_attribute($Key => { is => $rorw, isa => $MooseType, lazy => 0, required => 0, reader => sub { $_[0]->ORM->{$Key} }, writer => sub { $_[0]->ORM->newVal($Key, $_[1]) }, predicate => "has_$Key", }); I receive the error: bad accessor/reader/writer/predicate/clearer format, must be a HASH ref at /usr/local/lib/perl5/site_perl/mach/5.20/Class/MOP/Class.pm line 899 The reason of the error is clear: reader and

Correct way define and convert the Moose attribute type

a 夏天 提交于 2019-12-10 17:47:21
问题 Have: package MyPath; use strict; use warnings; use Moose; has 'path' => ( is => 'ro', isa => 'Path::Class::Dir', required => 1, ); 1; But want create this object with two ways, like: use strict; use warnings; use MyPath; use Path::Class; my $o1 = MyPath->new(path => dir('/string/path')); #as Path::Class::Dir my $o2 = MyPath->new(path => '/string/path'); #as string (dies - on attr type) And when call it with a 'Str' - want convert it internally in the MyPath package to Class::Path::Dir, so,

Make the Moose constructor ignore undef arguments

无人久伴 提交于 2019-12-10 16:08:29
问题 A hashtable is the typical initializer for your Perl objects. Now your input is unreliable in that you don't know whether for any given key there'll be a defined value, nor whether the key is there at all. Now you want to feed such unreliable input into Moose objects, and while absent keys are perfectly okay you do want to get rid of the undefined values so you don't end up with an object full of undefined attributes. You could certainly take great care when instantiating objects and filter

Perl Moose - Loading values from configuration file etc

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 10:23:11
问题 I'm new to using Moose, but I was wondering how I can load values from a configuration file and then exposing those values as properties of my 'config' object where the attributes are the configuration names in the configuration file. For example, The configuration file might contain: server:mozilla.org protocol:HTTP So I'd like my config object to have a 'server' attribute with a value of 'mozilla.org' and a protocol attribute with a value of 'HTTP'. Right now my understanding is that I have

C++-like usage of Moose with Perl for OOP

 ̄綄美尐妖づ 提交于 2019-12-09 06:22:38
问题 I've been playing around with Moose, getting a feel for it. I'd like an example of pure virtual functions like in C++ but in Moose parlance (specifically in a C++-looking way). I know that even with Moose imposing a stricter model than normal Perl, there's still more than one way to do what I'm asking (via method modifiers or SUPER:: calls). That is why I'm asking specifically for an implementation resembling C++ as much as possible. As for the "why?" of this restriction? Mostly curiosity,

How do I handle optional parameters in Moose?

北战南征 提交于 2019-12-08 17:50:20
问题 I'm currently starting with Perl OOP using the "Moose" package. The compiler complains that it "Can't modify non-lvalue subroutine call at Parser.pm line 16." I don't quite understand why I can't just assign a new object. I guess there is a better or more valid way to do optional parameters with Moose? #!/usr/bin/perl -w package Parser; use Moose; require URLSpan; require WWW::Mechanize; has 'urlspan' => (is => 'rw', isa => 'URLSpan', required => 1); has 'mech' => (is => 'rw', isa => 'WWW:

How to make the Moose constructor die on being passed an undeclared attribute?

走远了吗. 提交于 2019-12-08 16:05:03
问题 Moose is very tolerant by default. You can have a class named Cucumber and pass an undeclared attribute (like wheels ) to the constructor. Moose won't complain about that by default. But I might prefer Moose to rather die than accept undeclared attributes. How can I achieve that? I seem to remember having read it is possible but cannot find the place where it says so in the docs. package Gurke; use Moose; has color => is => 'rw', default => 'green'; no Moose; __PACKAGE__->meta->make_immutable

How can I modify a Moose attribute handle?

微笑、不失礼 提交于 2019-12-08 07:54:49
问题 Following phaylon's answer to "How can I flexibly add data to Moose objects?", suppose I have the following Moose attribute: has custom_fields => ( traits => [qw( Hash )], isa => 'HashRef', builder => '_build_custom_fields', handles => { custom_field => 'accessor', has_custom_field => 'exists', custom_fields => 'keys', has_custom_fields => 'count', delete_custom_field => 'delete', }, ); sub _build_custom_fields { {} } Now, suppose I'd like to croak if trying to read (but not write) to a non