moose

What's the proper way to create a BUILD method in MooseX::Declare?

I am having difficulty with the BUILD method in MooseX::Declare. If I say: #!/usr/bin/perl use MooseX::Declare; class Foo { has foo => (is => "rw", isa => "Str", default => "foo"); method BUILD { print "I was called\n"; } } Foo->new; I get the following less than helpful error message: Reference found where even...

How can I integrate Moose into Komodo?

ActiveState's Komodo is my preferred Perl IDE on OS X and XP. Recently I've begun coding new projects using Moose. Has anyone found a way to teach Komodo how to "identify" Moose's Attribute and Method declarations? I would just love to see Moose-Support in the "Code Browser/Code Explorer" of Komodo. ...

Perl and Moose: What Moose-based package should I use as replacement for MooseX::Method

To my dismay I noticed that MooseX::Method is no longer maintained and deprecated. The package MooseX-Method-Signatures is advertized as replacement, but its documentation says: This is ALPHA SOFTWARE. Use at your own risk. Features may change. <whine>What should I do </whine> ...

How can I code a factory in Perl and Moose?

Is there a simpler or better (=>easier to maintain) way to use Perl and Moose to instantiate classes based on incoming data? The following code is a stripped down sample from a project I'm working on. package FooBar; use Moose; has 'SUBCLASS' =>('isa'=>'Str',required=>'1',is=>'ro'); has 'MSG' =>('isa'=>'Str',required=>'1',is=>'ro'); s...

Is it possible to create an attribute that can only be set in the constructor in Moose?

Is it possible to create an attribute that can only be set in the constructor in Moose? I’d like to do something like this: my $foo = new Foo(file => 'foo.txt'); my $bar = new Foo(string => $str); $foo->file('baz.txt'); # dies I know I can create an attribute that can not be set in constructor, but the complementary case seems to be m...

C++-like usage of Moose with Perl for OOP

I've been playing around with Moose, getting a feel for it. I'd like an example of pure virtual functions like in C++ but in Moose parlance (specifically in a C++-looking way). I know that even with Moose imposing a stricter model than normal Perl, there's still more than one way to do what I'm asking (via method modifiers or SUPER:: cal...

How can I mock Moose objects?

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

What’s the proper way to create a BUILDARGS method using MooseX::Declare?

I'm having difficulty using MooseX::Declare properly when calling BUILDARGS. I'm trying to create an object as an interface for a file. (Specifically, I want an interface to a binary file that lets me peek at the next few bytes in the file then chomp them off for further processing.) I want to be able to create one of these objects li...

How can I improve Moose performance in non-persistent CGI processes?

Moose is a fantastic object framework. The trouble is that, taken together with its dependencies, it's very big. Our profiling indicates that on our platform, simply loading Moose will incur a 5-6 second overhead on non-persistent CGI application scripts. That's just not acceptable for these one-off applications. By contrast, when we...

In Moose, how do I modify an attribute any time it is set?

If you have an attribute that needs to be modified any time it is set, is there a slick way of doing this short of writing the accessor yourself and mucking around directly with the content of $self, as done in this example? package Foo; use Moose; has 'bar' => ( isa => 'Str', reader => 'get_bar', ); sub set_bar { my ($sel...

How can I declare a class variable as floating point in Moose?

How can I declare a class variable as floating point in Moose? My (fictional) sample below produces errors for "Real", "Number" etc ... "Str" works but defeats the purpose .. searching/Google doesn't help, since I can't hit the correct search terms... PROBLEM.pm package PROBLEM; use strict; use warnings; use Moose; has 'PROBLEM'=> ...

Do MooseX::AttributeHelpers and MooseX::FollowPBP interact correctly?

The following code defines two classes (DeckA and DeckB) that differ only in whether they use the features that come with MooseX::AttributeHelpers. The getters generated by Moose for DeckB are not what I expected. Is this a bug or am I misunderstanding how MooseX::AttributeHelpers and MooseX::FollowPBP ought to interact? My workaround f...

How do I best make triggered accessors with defaults in Moose?

I have a situation where I'd like to cache some calculations for use later. Let's say I have a list of allowed values. Since I'm going to be checking to see if anything is in that list I'm going to want it as a hash for efficiency and convenience. Otherwise I'd have to grep. If I'm using Moose it would be nice if the cache was recalc...

How do I install the dependencies I need for MySQL::Slurp?

I am trying to install the mysqlslurp utility found in MySQL::Slurp. I found that it requires Moose and therefore I installed that package too. But I am still not able to use the mysqlslurp command. I get an error: Can't locate Moose.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi ..... BEGIN fa...

Problem with mixins in a MooseX::NonMoose class

Consider the following: package MyApp::CGI; use Moose; use MooseX::NonMoose; use Data::Dumper; extends 'CGI::Application'; BEGIN { print "begin isa = " . Dumper \@MyApp::CGI::ISA; }; print "runtime isa = " . Dumper \@MyApp::CGI::ISA; ... The output when this compiles is: begin isa = $VAR1 = [ 'Moose::Object' ...

How can I extend Moose's automatic pragma exports?

You know how Moose automatically turns on strict and warnings during import? I want to extend that behavior by turning on autodie and use feature ':5.10' in my Moose classes. I've tracked down where Moose does this, in Moose::Exporter, which assembles a custom import sub for Moose that calls strict->import and warnings->import for the c...

Perl Moose Method Modifiers: Call 'around' before 'before' and 'after'

I'm using Moose and I need to wrap method calls in my project. It's important that my wrapping code be the most outer modifier. What I've done so far is put my method modifiers in a Moose Role and then applied that role at the end of my class like this: use Moose::Util; Moose::Util::apply_all_roles(__PACKAGE__->meta, ('App:Roles::Custom...

How can I access the meta class of the module my Moose role is being applied to?

I'm using Moose roles to apply some wrapper behaviour around some accessor methods in a class. I want to apply this role to a number of modules, each of which have a different set of attributes whose accessors I want to wrap. Is there a way to access the meta class of the module being applied to, from within the role? i.e. something li...

Resources for getting started on "modern" Perl

After having heard about new parts of the Perl ecosystem, such as Moose, DeclareX, and Catalyst, I thought that it'd be nice to take a look at Perl. Unfortunately, all of the introductory material I can find targets Perl 5.8 or 5.6, and knows nothing about these newer frameworkslet alone features introduced in recent Perl versions, such...

How do I create a cyclic graph of immutable objects in Perl and Moose?

This could seem like an obviously hopeless case, but is there a trick to create a cyclic graph of immutable objects in Perl? Something like this: package Node; use Moose; has [qw/parent child/] => (is => 'ro', isa => 'Node'); package main; my $a = Node->new; my $b = Node->new(parent => $a); Now if I wanted $a->child to point to $b, w...