moose

How can I provide an alternate init arg for an attribute in Moose?

随声附和 提交于 2019-12-05 02:23:09
问题 I of course know that I can rename the init arg for an attribute by setting init_arg (e.g) package Test { use Moose; has attr => ( is => 'ro', isa => 'Str', init_arg => 'attribute' ); } which would allow me to Test->new({ attribute => 'foo' }); but not Test->new({ attr => 'foo' }); at the same time MooseX::Aliases actually has this behavior, but creating an alias also creates accessors. I'm currently trying to understand the code in that module to see if I can't determine how it does it, so

Why doesn't “use overload” work with “use namespace:autoclean”?

▼魔方 西西 提交于 2019-12-05 00:35:43
Ok just to sanity check overload doesnt seem to be working for me. I don't know if it's the version of perl I have, or the version of overload.pm, or something wrong with how I've implemented it, but this code doesnt work for me. perl version This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi overload version perl -Moverload -e 'print "$overload::VERSION\n";' 1.07 Token.pm package Token; use namespace::autoclean; use Data::Dumper; use Moose; use Moose::Util::TypeConstraints; use overload '+' => \&_overload_add, fallback => 1; has 'secretvalue' => ( is => 'rw', isa => 'Int'); sub

In Perl/Moose, how can I apply a modifier to a method in all subclasses?

混江龙づ霸主 提交于 2019-12-05 00:29:54
I have a Moose class that is intended to be subclassed, and every subclass has to implement an "execute" method. However, I would like to put apply a method modifier to the execute method in my class, so that it applies to the execute method in all subclasses. But method modifiers are not preserved when a method is overriden. Is there any way to ensure that all subclasses of my class will have my method modifier applied to their execute methods? Example: In a superclass, I have this: before execute => sub { print "Before modifier is executing.\n" } Then, in a subclass of that: sub execute {

What should I do if a Moose builder method fails?

ⅰ亾dé卋堺 提交于 2019-12-04 19:38:40
问题 What is the best way to handle a failure in a builder method? For example: package MyObj; use Moose; use IO::File; has => 'file_name' ( is => 'ro', isa => 'Str', required =>1 ); has => 'file_handle' ( is => 'ro', isa => 'IO::File', lazy_build => 1 ); sub _build_file_handle { my $self = shift; my $fh = IO::File->new( $self->file_name, '<' ); return $fh; } If the _build_file_handle fails to get a handle, the builder will return undef , which fails the type constraint. I could use a union in the

How can I mock Moose objects?

大城市里の小女人 提交于 2019-12-04 18:31:59
问题 What strategies have Perl people used when mocking Moose objects that they will inject into other Moose objects as type-constrained attributes? Test::MockObject::Extends doesn't seem to play well with Moose. I need the object to blessed as a specific package though so a vanilla Test::MockObject won't work. I'm sure other folks have had similar difficulty. How did you resolve it? Extra Points for Solutions that are already on CPAN. 回答1: Well I'm not the expert on such things but the first

Perl::Critic: Life after Moose?

若如初见. 提交于 2019-12-04 17:06:35
问题 I've started a conversion of a project to Moose and the first thing I noticed was that my critic/tidy tests go to hell. Moose, Tidy and Critic don't seem to like each other as much as they used to. Are there docs anywhere on how to make critic/tidy be more appreciative of the Moose dialect? What do most Moose users do? Relax/ditch critic for the more heavy Moose modules? Custom policies? 回答1: Have you seen Perl::Critic::Moose? 回答2: Both of them can be configured into detail. I have no idea

Coercing ArrayRef[MyClass] from ArrayRef[HashRef]

若如初见. 提交于 2019-12-04 14:14:01
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 use warnings; use strict; package Company; use Moose; use Moose::Util::TypeConstraints; has 'id' =>

How to handle mocking roles in Moose?

梦想与她 提交于 2019-12-04 11:34:14
Say that I have two roles: Simple::Tax and Real::Tax. In testing situations, I want to use Simple::Tax, and in production, I want to use Real::Tax. What is the best way to do this? My first thought was to use different versions of the new method to create objects with different roles: #!/usr/bin/perl use warnings; { package Simple::Tax; use Moose::Role; requires 'price'; sub calculate_tax { my $self = shift; return int($self->price * 0.05); } } { package A; use Moose; use Moose::Util qw( apply_all_roles ); has price => ( is => "rw", isa => 'Int' ); #price in pennies sub new_with_simple_tax {

How to store Hash of Hashes in Moose?

女生的网名这么多〃 提交于 2019-12-04 11:25:39
i was wondering, what is the best way to store Hash of Hashes in Moose. Lets take for example a Hash like this: my %hash = ('step1' => {'extraction' => \$object1, 'analysis' => \$object2}, 'step2' => {'extraction' => \$object3, 'analysis' => \$object4}); but i want to save this one in a moose attribute. How should i organize the access (reading, writing) on this. Examples on the net are mostly for "flat" hashes. But then you can use helpers like Moose::Meta::Attribute::Native::Trait::Hash. Is there something similar for hash of hashes? Reason for this is, that i want to iterate over the step

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

本秂侑毒 提交于 2019-12-04 09:32:53
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; __PACKAGE__->meta->make_immutable(); 1; It is working, so my $d = Local->new(pathstr => '/tmp')->path