moose

How can I create internal Moose object variables?

I would like some attributes (perhaps this is the wrong term in this context) to be private, that is, only internal for the object use - can't be read or written from the outside. For example, think of some internal variable that counts the number of times any of a set of methods was called. Where and how should I define such a variabl...

Do all my Moose classes have to contain 'namespace::autoclean' and 'make_immutable' or is there some way to get these by default?

According to the Moose best practices doc, my Moose classes should look like this: package Person; use Moose; use namespace::autoclean; # extends, roles, attributes, etc. # methods __PACKAGE__->meta->make_immutable; 1; See Moose::Manual::BestPractices. And 99% of the time this is what I want, so is there some way to have my name...

When should I use `use`?

As long as I can remember, whenever I use a module I include a use line at the beginning of my code. Recently I was writing two Moose object modules that use each other. Look at this over-simplistic example: One module: package M1 0.001; use Moose; use 5.010; use namespace::autoclean; # use M2; ### SEE QUESTION BELOW has 'name' => ...

How can I overload Moose constructors?

Sorry for the Java jargon, but how can I overload Moose constructors? Suppose I'm representing a segment. I can either take a start point and and point, or a start point and length, or end point and length. How can I allow for such alternative construction methods? ...

How can I modify a Moose attribute handle?

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_fiel...

How should I serialize an array of Moose objects?

I use MooseX::Storage for serialization of Moose objects. Can I use it for serialization of multiple Moose objects to the same file, or more specifically, an array or a hash of Moose objects? I guess I can define another Moose objects ('array_of_myobj') but this isn't very elegant. So, how would you recommend to serialize an array (or ...

What are 'weak references' in KiokuDB?

What exactly are the weak references that KiokuDB tutorial mentions? How do they differ from 'normal' references? ...

How should I copy objects between two KiokuDB dirs?

I would like to make sure I get KiokuDB's scope concept correctly. Suppose I would like to load an object from db1 and store it in db2. Must I have both scopes 'open' at the same time? ...

How can I require a Moose constructor arg that is not a attribute?

I have a Moose object module that should accept a relatively large data structure (ds) as one of its constructor arguments. It is used to calculate some of the object's attributes. However, I do not wish to store ds itself as an attributes -- it is only needed during the construction of the object. I thought of using BUILDARGS but then ...

How can I make my Perl method more Moose-like?

I am practising Kata Nine: Back to the CheckOut in Perl whilst also trying to use Moose for the first time. So far, I've created the following class: package Checkout; # $Id$ # use strict; use warnings; our $VERSION = '0.01'; use Moose; use Readonly; Readonly::Scalar our $PRICE_OF_A => 50; sub price { my ( $self, $items ) = @...

Which KiokuDB backend is suitable for my serialization needs?

I use KiokuDB to store a couple of Moose objects and a couple of simple array structures (hashes and arrays). I do not need any fancy searches, transactions etc., simple the ability to fetch (lookup) an object. Also, as soon as I'm done creating the DB, it can be set read-only. No changes will ever be made to it. The main (only?) reaso...

Is there a reason I should NOT serialize my (Moose) objects using Storable or YAML?

I have a few Moose objects and some other simple hash objects (hashes, arrays) I'd like to serialize. At first, I used a simple my $obj_store_file = nstore($obj); and my $obj = retrieve($obj_store_file); This worked well. Later, I found about MooseX::Storage and KiokuDB. I tried using them to enjoy some benefits they have, but: ...

Why do I have to load a Perl class to use its object I deserialize from YAML?

I was trying to serialize some (Moose) objects with YAML -- simply by using YAML's Dump() and Load(). After loading a serialized object, it didn't 'work' until I added a use statement with the original module name. If I don't use use I won't get any error until I try to invoke some object method, then it will croak saying it can't find ...

Moose::Role - easy way to augment application of the role?

I have a Moose::Role that I would like to call some extra subs on the class when that role is applied to the class. Is there an easy way to modify what happens when the role is applied, without having to dig too much into Moose::Meta::Role type coding? Ideally, I'd just like to after 'apply' => ... to add the extra stuff. Edit: I'm s...