moose

How can I set a static variable that can be accessed by all subclasses of the same base class (Perl/Moose)?

回眸只為那壹抹淺笑 提交于 2019-12-29 07:39:19
问题 Since Perl/Moose always calls the base class' BUILD function before it calls the subclass BUILD function, there is a new instance of the base class everytime you instantiate a subclass. How do I go about creating a static variable that can be used by all the subclasses, or alternatively how can I create a static base or abstract class? (does that approach even make sense?) I'm trying to create a variable that dynamically enables or disables certain features of a function defined at run-time

Migrating from Moose to Mouse in Perl - Mouse not executing BUILD

烈酒焚心 提交于 2019-12-24 21:42:35
问题 I'm trying to migrate from Moose to Mouse in the interests of speed but have encountered a showstopper error. I'm building two objects in the same scope: sub scope { my $foo = Foo->new(); my $bar = Bar->new(); } The BUILD method of Foo is firing but the BUILD method of Bar is not. Any ideas? Both Foo and Bar inherit from Baz which inherits from Mouse::Object. 回答1: You're not really providing enough context for anybody to debug this. Also I'm worried about your comment migrating from Moose to

How to update a Perl/Tk widget textvariable that is a moose attribute?

淺唱寂寞╮ 提交于 2019-12-24 00:56:05
问题 In Perl/Tk, one can define textvariables for widgets. It's a reference to some scalar that holds the value. Someone showed me how to use Moose attribute coercion to use Moose attributes as textvariable (cool!). This is how it works: subtype 'TkRef' => as 'ScalarRef'; coerce 'TkRef', from 'Str', via { my $r = $_; return \$r }; has 'some_val' => (is => 'rw', isa => 'TkRef', coerce => 1, default => 'default value'); $mw->Entry(-textvariable => $self->some_val); $mw->Label(-textvariable => $self-

How do I serialize Moose objects to XML?

不打扰是莪最后的温柔 提交于 2019-12-23 20:51:39
问题 I have a bunch of legacy modules I want to convert to being Moose-based. The modules currently have "toXML" methods, which are hand-coded using XML::LibXML. Is there a module or technique for serializing Moose objects to XML? I have looked at MooseX::Storage, but that handles JSON, YAML, and Storage, not XML. A google search for Moose and XML yields lots of references to XML::Rabbit, which seems to be good for parsing XML into Moose classes, but there's not a lot out there for taking Moose

How should I serialize an array of Moose objects?

一笑奈何 提交于 2019-12-23 17:11:12
问题 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 a hash) of Moose objects? 回答1: You don't have to let MooseX::Storage manage your file IO just because it's available. You could use it to pack your objects into

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

元气小坏坏 提交于 2019-12-23 03:24:19
问题 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'.

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

北慕城南 提交于 2019-12-22 17:39:31
问题 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

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

泪湿孤枕 提交于 2019-12-22 14:59:32
问题 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

How to instantiate Moose classes from a big hash

时光毁灭记忆、已成空白 提交于 2019-12-22 10:43:38
问题 I have a big hash many levels deep, and I'd like to turn this hash into a set of Moose classes. The hash looks something like this: my %hash = ( company => { id => 1, name => 'CorpInc', departments => [ { id => 1, name => 'Sales', employees => [ { id => 1, name => 'John Smith', age => '30', }, ], }, { id => 2, name => 'IT', employees => [ { id => 2, name => 'Lucy Jones', age => '28', }, { id => 3, name => 'Miguel Cerveza', age => '25', }, ], }, ], } ); And the Moose classes: package Company;

Perl internals and Moose: constant-folding optimization

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:40:02
问题 I've been curious about constant-folding optimizations which Perl performs, but it occurred that when the code has Moose involved chances are that constant-folding won't be performed (please correct me if I am wrong). I have Moose code which contains the method as below: sub foo { my ($self) = shift; my $test_y = $self->pos->[1]; #... if ($self->is_map_val($self->pos->[0]+8, $test_y+32) || $self->is_map_val($self->pos->[0]+32-8, $test_y+32)) { { heavy_stuff(); } #... } and when I run perl -MO