moose

How to share an attribute between multiple Moose objects?

风格不统一 提交于 2019-12-08 04:05:53
问题 I am in need of guidance. I've been switching my lifestyle over to Moose as of late. Overall, I've found that moose makes life much more interesting and fun. At the same time, I still haven't gotten to a point where I feel more productive... probably because I keep losing myself in abstraction and still haven't learned enough to be able to parse the moose docs effectively enough to know what other smarter folks have solved already. I did zero OO programming before moose walked into my life.

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

断了今生、忘了曾经 提交于 2019-12-07 17:04:07
问题 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

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

倾然丶 夕夏残阳落幕 提交于 2019-12-07 14:05:08
问题 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

How can I modify a Moose attribute handle?

不想你离开。 提交于 2019-12-07 07:28:26
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-existing custom field. I was suggested by phaylon to wrap custom_field with an around modifier. I have

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

扶醉桌前 提交于 2019-12-07 05:32:03
问题 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' => (

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

给你一囗甜甜゛ 提交于 2019-12-06 18:47:39
问题 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;

Can I use an attribute modifer in Moose in a base class to handle multiple attributes from sub classes?

≡放荡痞女 提交于 2019-12-06 14:55:20
I've got several Modules that have a number of date time related attributes that accept a datestamp and return a human readable string ( dd Mon yyyy hh:mm:ss ). package ModuleOne; use Moose; extends 'ModuleBase'; has date_attr_one => ( ... ); and then ... package ModuleTwo; use Moose; extends 'ModuleBase'; has date_attr_mouse => ( ... ); As it's currently working I'm using an 'around' attribute modifier so that if there's no parameter return the a date in the format of '03 May 2012 12:33:42'. If there is a parameter in the format of '03 May 2012 12:33:42' the I want to set that as a datestamp.

How do I turn Moose objects into JSON for use in Catalyst?

北城以北 提交于 2019-12-06 12:07:20
I have a series of Moose objects that I'm looking to feed to JSON::XS by way of Catalyst::View::JSON . JSON::XS is unable to encode blessed data-structures. I know that there is MooseX::Storage::Format::JSON which can -- kinda -- do what I want; but, it seems pretty overly heavy. What I'm looking for is essentially the same information that XXX.pm provides. I just want the raw-data structures recursively unblessed so JSON::XS (the driver for JSON::Any that C:V:JSON uses internally) can display it. What is the best way go about using Catalyst::View::JSON and JSON::XS with Moose objects? It

In Perl, how do I put multiple class in a single .pm file

依然范特西╮ 提交于 2019-12-06 10:23:21
I have a question of Perl MooseX::Declare . I want to put multiple classes in a single .pm file. How do I make this file, and what will the filename be because that .pm file has multiple classes? For example use MooseX::Declare; class Point { has 'x' => ( isa => 'Num', is => 'rw' ); has 'y' => ( isa => 'Num', is => 'rw' ); method clear { $self->x(0); $self->y(0); } method set_to( Num $x, Num $y) { $self->x($x); $self->y($y); } } class Point3D { has 'z' => ( isa => 'Num', is => 'rw' ); method clear { $self->z(0); } method set_to( Num $x, Num $y, Num $z) { $self->x($x); $self->y($y); $self->z($z

How does one get a method reference when using Moose

喜夏-厌秋 提交于 2019-12-06 09:38:51
I'm trying to figure out how to get a method code reference using Moose. Below is an example of what I'm trying to do: use Modern::Perl; package Storage; use Moose; sub batch_store { my ($self, $data) = @_; ... store $data ... } package Parser; use Moose; has 'generic_batch_store' => ( isa => 'CodeRef' ); sub parse { my $self = shift; my @buf; ... incredibly complex parsing code ... $self->generic_batch_store(\@buf); } package main; $s = Storage->new; $p = Parser->new; $p->generic_batch_store(\&{$s->batch_store}); $p->parse; exit; Eric Strom The question I linked to above goes into detail