moose

Can anybody please explain (my $self = shift) in Perl

淺唱寂寞╮ 提交于 2019-12-02 17:40:40
I'm having a really hard time understanding the intersection of OO Perl and my $self = shift; The documentation on these individual elements is great, but none of them that I've found touch on how they work together. I've been using Moose to make modules with attributes, and of course, it's useful to reference a module's attribute within said module. I've been told over and over again to use my $self = shift; within a subroutine to assign the module's attributes to that variable. This makes sense and works, but when I'm also passing arguments to the subroutine, this process clearly takes the

How do I make a new Moose class and instantiate an object of that class at runtime?

て烟熏妆下的殇ゞ 提交于 2019-12-02 12:38:37
问题 After creating a metaclass using Moose::Meta::Class->create , how do I instantiate a real Moose class with that class as a metaclass? (I need to create the metaclass also because I also want to apply some roles to it.) 回答1: The metaclass is the class, of course. If you want an instance of that class, just do: my $instance = $meta->name->new You might also need to make sure that $meta doesn't get collected too soon. Generally, you do this: $meta->add_method( meta => sub { $meta } ); That will

Pass variables around the around method modifier

梦想与她 提交于 2019-12-02 08:23:46
问题 Is it possible to pass variables between multiple calls to the around MethodModier? example (that doesn't work but hopefully conveys what I want to do) sub mysub { ... }; around 'mysub' => sub { my $orig = shift; my $self = shift; my $value = get_value; $self->orig(@_); }; around 'mysub' => sub { my $orig = shift; my $self = shift; my $value = shift; my $output = "sometext $value" . $self->orig(@_); . 'someothertext $value' ; }; I'd eventually like to have these 'arounds' placed in pluggable

Dynamically loading Perl modules

陌路散爱 提交于 2019-12-01 21:55:53
I am trying to make an extensible system whereby I can code up a new module to be a handler. I want the program to automatically load any new .pm file that is put into the Handlers directory and conforms to a Moose::Role interface. I'm wondering whether there is a Perl module or a more Moose sanctioned way to do this automatically? Here is what I have built so far, but it seems a little verbose and there has got to be a simpler way to do it. handler.pl contains: #!/usr/bin/perl use Handler; use Data::Dumper; my $base_handler = Handler->new(); $base_handler->load_modules('SysG/Handler'); print

How to have Moose return a child class instance instead of its own class, for polymorphism

大兔子大兔子 提交于 2019-12-01 17:51:17
I want to create a generic class, whose builder would not return an instance of this generic class, but an instance of a dedicated child class. As Moose does automatic object building, I do not get to understand if this something possible, and how to create a Moose class with Moose syntax and having this behaviour. e.g.: The user asks: $file = Repository->new(uri=>'sftp://blabla') .... and is returned an `Repository::_Sftp`` instance User would use $file as if it is a Repository instance, without the need to know the real subclass (polymorphism) Note: As requested, maybe i should have been

Which recommended Perl modules can serialize Moose objects?

痞子三分冷 提交于 2019-12-01 09:26:12
I was usually using Storable with nstore , but now I have a module that has CODE and apparently Storable doesn't like that. I found YAML (and YAML::XS which I can't really get to work ). I also experimented a bit with MooseX::Storage without much success. Are there other alternatives? What would you recommend? Ether You can dump a coderef with Data::Dumper after setting $Data::Dumper::Deparse to a true value, but this is only intended for debugging purposes, not for serialization. I would suggest you go back to looking at why MooseX::Storage isn't working out for you, as the authors tried

How to use a Moose class defined in the same file as the main script?

江枫思渺然 提交于 2019-12-01 07:33:28
The following script p.pl works fine: use feature qw(say); use strict; use warnings; use lib '.'; use P1; my $obj = P1->new(name => 'John'); say "The name is: ", $obj->name; where the class P1 is defined in file P1.pm : package P1; use Moose; has name => (is => 'rw', isa => 'Str'); 1; However, when I try to move the class P1.pm into the main script: #! /usr/bin/env perl use feature qw(say); use strict; use warnings; my $obj = P1->new(name => 'John'); say "The name is: ", $obj->name; package P1; use Moose; has name => (is => 'rw', isa => 'Str'); I get error: Can't locate object method "name"

How to use a Moose class defined in the same file as the main script?

喜欢而已 提交于 2019-12-01 04:48:21
问题 The following script p.pl works fine: use feature qw(say); use strict; use warnings; use lib '.'; use P1; my $obj = P1->new(name => 'John'); say "The name is: ", $obj->name; where the class P1 is defined in file P1.pm : package P1; use Moose; has name => (is => 'rw', isa => 'Str'); 1; However, when I try to move the class P1.pm into the main script: #! /usr/bin/env perl use feature qw(say); use strict; use warnings; my $obj = P1->new(name => 'John'); say "The name is: ", $obj->name; package

Writing to read-only attributes inside a Perl Moose class

China☆狼群 提交于 2019-12-01 02:58:04
问题 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 => 'Str'); has 'age' => (is => 'ro', isa => 'Int'); sub HAPPY_BIRTHDAY { my $self = shift; $self->{age}++; # Age is accessed through method 1 } sub HAPPY_BIRTHDAY2 { my $self = shift; my $age = $self->age(); $self->age($age + 1); # Age is accessed

Is Moose really this slow?

走远了吗. 提交于 2019-12-01 00:01:13
问题 I recently downloaded Moose. Experimentally, I rewrote an existing module in Moose. It seems to be convenient way to avoid writing lots of repetitive code. I ran the tests of the module, and I noticed it was a bit delayed. I profiled the code with -d:DProf and it seems that just including the line no Moose; in the code increases the running time by about 0.25 seconds (on my computer). Is this typical? Am I doing something wrong, did I misinstall it, or should we really expect this much delay?