moose

Coercing ArrayRef[MyClass] from ArrayRef[HashRef]

无人久伴 提交于 2019-12-06 07:18:32
问题 In trying to answer How to instantiate Moose classes from a big hash, I think I have hit another place where I don't fully understand Moose type coercions. For some reason, the below code issues warnings: You cannot coerce an attribute (departments) unless its type (ArrayRef[Company::Department]) has a coercion at ./test.pl line 12. You cannot coerce an attribute (employees) unless its type (ArrayRef[Company::Person]) has a coercion at ./test.pl line 23. but then succeeds. #!/usr/bin/env perl

Path::Class::File or ::Dir & Moose initialiazation and coercion

不打扰是莪最后的温柔 提交于 2019-12-06 06:47:16
问题 Currently have: package Local; use warnings; use Moose; use Method::Signatures::Simple; use Path::Class::File; use Path::Class::Dir; method _build_path_class { my $str = $self->pathstr; return Path::Class::Dir->new($str) if (-d $str); return Path::Class::File->new($str); } has 'pathstr' => (is => 'rw', isa => 'Str', required => 1); has 'path' => ( is => 'rw', lazy => 1, #isa => 'Path::Class::File|Path::Class::Dir', #this didn't work isa => 'Object', builder => '_build_path_class', ); no Moose

Moose: Expiring cached results of calculations when attribute values change?

情到浓时终转凉″ 提交于 2019-12-06 04:02:54
问题 In our classes we have a pattern where we create an attribute to represent a calculated value. For obvious reasons we want to cache the calculated value and then invalidate the cache when one of the underlying values change. So we currently have this: package FooBar; use Moose; has 'foo' => ( accessor => { 'foo' => sub { my $self = shift; if (@_ > 0) { # writer $self->{foo} = $_[0]; # reset fields that are dependant on me $self->{bar} = undef; } # reader part; return $self->{foo}; } } ); has

How to instantiate Moose classes from a big hash

别来无恙 提交于 2019-12-06 01:30:57
I have a big hash many levels deep, and I'd like to turn this hash into a set of Moose classes. The hash looks something like this: my %hash = ( company => { id => 1, name => 'CorpInc', departments => [ { id => 1, name => 'Sales', employees => [ { id => 1, name => 'John Smith', age => '30', }, ], }, { id => 2, name => 'IT', employees => [ { id => 2, name => 'Lucy Jones', age => '28', }, { id => 3, name => 'Miguel Cerveza', age => '25', }, ], }, ], } ); And the Moose classes: package Company; use Moose; has 'id' => (is => 'ro', isa => 'Num'); has 'name' => (is => 'ro', isa => 'Str'); has

Perl Moose - Loading values from configuration file etc

◇◆丶佛笑我妖孽 提交于 2019-12-06 01:14:35
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 to explicitly name the attributes with a has 'server' => ( is => 'ro', isa => 'Str', default =>

Use or not to use the namespace::sweep and/or Modern::Perl

感情迁移 提交于 2019-12-05 22:16:58
In my last question @Borodin commented my question with: You should start by removing Modern::Perl and namespace::sweep. Modules that behave as pragma should be avoided. I'm confused a bit, because: in the LATEST Moose BestPractices manual recommending to use namespace::autoclean . The use namespace::autoclean bit is simply good code hygiene, as it removes imported symbols from your class's namespace at the end of your package's compile cycle, including Moose keywords. Once the class has been built, these keywords are not needed. (This is preferred to placing no Moose at the end of your

What's the best way to assign a method body at construction time when using Moose?

邮差的信 提交于 2019-12-05 20:24:34
I'm using Moose (specifically MooseX::Declare ) to create an iterator object, Iter which has a next method that advances state and returns 0 or 1 as required for use in a while statement. The problem I'm running into is that depending on the existence of one of the construction parameters, next needs to perform two very different sets of operations. The way I see it I have five options: if ... then in next method subclass hash dispatch symbol table manipulation put methods in different modules and load required one at construction time Number 1 is just amateur. Number 2 is, I suppose, the

OO Design Patterns with Perl

ⅰ亾dé卋堺 提交于 2019-12-05 12:44:00
I am currently planning the design for a new system I will need to code that interacts with a back-end API. I was contemplating object composition and inheritance and decided that the most correct procedure in my situation would be to go with composition over inheritance as my objects have a "has a" relationship to one another and not an "is a". I find now though that because some objects are reliant on other, there may be cases were "object A" has an attribute which is "object B" and an attribute "object C" - however "object B" also has an attribute "object C". In hope that this analogy would

Moose: How to get an array of objects? Traits?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 10:08:28
I'm beginning to realize that this is for beginners: package Bad; has 'arr' => ( is => 'rw', 'ArrayRef[Str]' ); package main; my $bad = Bad->new(arr => [ "foo", "bar" ]); print $bad->arr->[0], "\n"; Enter traits. I'm underwhelmed by the traits API, though. Have I misunderstood something? Can I get this API instead somehow? : print $bad->arr->get(0), "\n"; Details Review the canonical traits example from Moose::Meta::Attribute::Native::Trait::Array package Stuff; use Moose; has 'options' => ( traits => ['Array'], is => 'ro', isa => 'ArrayRef[Str]', default => sub { [] }, handles => { all

Is it “OK” to wrap standard Perl modules with Moose?

会有一股神秘感。 提交于 2019-12-05 09:57:25
Many standard modules are all using straight up perl -- problem is these guys arent using Moosey stuff, so I catch myself wrapping them with Moose or reinventing some simple functions in bigger libraries for convenience. I wondered if there was any general approach to how developers using Moose incorporate other libraries that are non-Moose. Being new to Perl and Moose I'd like to have a better understanding of how Moose is used in situations like this, or when it is generally preferred to use Moose vs Perl or even MooseX, or some other package, or whether its arbitrary. Seems like there are