Questions tagged [moose]

Moose is a postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl. It borrows all the best features from Perl 6, CLOS (Lisp), Smalltalk, Java, BETA, OCaml, Ruby and more, while still keeping true to its Perl 5 roots.

Moose is an extension of the Perl 5 object system. The main goal of Moose is to make Perl 5 object-oriented programming (OOP) easier, more consistent and less tedious. With Moose you can think more about what you want to do and less about the mechanics of OOP.

Moose links

See Also

sources: moose.perl.org

609 questions
6
votes
2 answers

Can I make a Moose attribute "write once"?

I would like to have a non-required Moose attribute that can be set only once. If I use is => 'ro' I must set the attribute upon creation of the object, but I want to be able to add it afterwards (as long as it's not been set already).
David B
  • 29,258
  • 50
  • 133
  • 186
6
votes
2 answers

How can I build multiple attributes with a single builder in Moose?

Using Moose, is it possible to create a builder that builds multiple attributes at once? I have a project in which the object has several 'sets' of fields - if any member of the set is requested, I want to go ahead and populate them all. My…
RickF
  • 1,812
  • 13
  • 13
6
votes
2 answers

Deep cloning Moose object with attributes that are ArrayRef[Object] and Set::Object

I've got a Moose object: class My::Game { has 'players' => (isa => 'Set::Object', ...) has 'action_sequence' => (isa => 'ArrayRef[My::Game::Action]', ...) } Now I want to be able to clone this object with a call like $game2 = $game->clone; How…
PBJ
  • 757
  • 8
  • 21
6
votes
2 answers

Argument for builder subroutine in a moose object

I'm currently delegating the builder method to all of the objects that extend one of my base classes. The problem that I'm facing is I need all of the objects to either read an attribute of itself or be passed in a value. # In Role: has…
lespea
  • 107
  • 2
  • 6
6
votes
3 answers

Writing to read-only attributes inside a Perl Moose class

Using Perl and Moose, object data can be accessed in 2 ways. $self->{attribute} or $self->attribute() Here is a simple example demonstrating both: # Person.pm package Person; use strict; use warnings; use Moose; has 'name' => (is => 'rw', isa =>…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
6
votes
3 answers

In Moose subroutines, how does $meta get into @_?

chromatic's recent blog got me curious about the Moose subroutine has. I was looking at the Moose source code and noticed that inside the has subroutine, there is a $meta variable unpacked from @_. Where does $meta come from? I've started wading…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
6
votes
3 answers

How do I compose an existing Moose role into a class at runtime?

Say I define an abstract My::Object and concrete role implementations My::Object::TypeA and My::Object::TypeB. For maintainability reasons, I'd like to not have a hardcoded table that looks at the object type and applies roles. As a DWIMmy example,…
Oesor
  • 6,632
  • 2
  • 29
  • 56
6
votes
2 answers

Real advantages of using Moo(se) over Perl OO

I am currently working at a company, where we are doing Perl development. However the code is really messy, uses really old Perl idioms, so I've decided to slowly clean it up and teach my coworkers about Modern::Perl, good software design, OOP -…
Davs
  • 489
  • 4
  • 12
6
votes
3 answers

Is there a simple way to test if a Moose attribute is read-only?

I currently use a block eval to test that I've set an attribute as read-only. Is there a simpler way to do this? Example from working code: #Test that sample_for is ready only eval { $snp_obj->sample_for('t/sample_manifest2.txt');}; like($@,…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
6
votes
3 answers

How do you replace a method of a Moose object at runtime?

Is it possible to replace a method of a Moose object at runtime ? By looking at the source code of Class::MOP::Method (which Moose::Meta::Method inherits from) I concluded that by doing $method->{body} = sub{ my stuff } I would be able to replace…
xxxxxxx
  • 5,037
  • 6
  • 28
  • 26
6
votes
2 answers

How do I reduce number of redundant requests with mod_perl properly?

In a fairly big legacy project, I've refactored several hairy modules into Moose classes. Each of these modules requires database access to (lazy) fetch its attributes. Since those objects are used pretty heavily, I want to reduce the number of…
Nikolai Prokoschenko
  • 8,465
  • 11
  • 58
  • 97
6
votes
2 answers

Moose Triggers Not Firing When Using DBIX::Class

I am new to Moose and am trying to use it with DBIx::Class. Basic DBIC querying and updating work find, but any trigger I attempt to write does not get executed when I modify an attribute. use Modern::Perl; use Data::Dumper; my $schema =…
user287388
6
votes
3 answers

Is there an easy way to map DBIx::Class results to my custom Moose classes?

It seems kind of a pain to have my Moose classes. Then to use DBIx::Class to get a result set..then to manually map my result set to the moose classes.
mike park
  • 61
  • 2
6
votes
1 answer

Moose attribute initialization

What is the typical approach for custom initialization of certain attributes when using Moose? For instance, suppose I take two dates in string format as input to my class: has startdate => (is => 'ro', isa => 'Str', required => 1); has enddate …
Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
6
votes
2 answers

Is there a vim plugin that makes Moose attributes show up in Tag_List?

I am editing packages that use Moose, and I was wondering if there were a plugin for making Moose attributes show up in the Tag List. For example, in the following code, the attribute options does not show up in Tag_List, but print_out_site…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99