I have just released a MooseX::AttributeCloner to the CPAN. The purpose of this role is to go pass all built attribute values from one class object to a newly instantiated object.
The reason for the creation of this role is for our pluggable pipeline system, where we want to be able to pass any parameters from the command line to any of the objects which are responsible for submitting jobs.
The Role inspects all the attributes the class has via meta, and checks that the attribute is defined. If it is, it passes the value into a hash, which it then uses in the object instantiation of the new object.
my $object_with_attributes = $self->new_with_cloned_attributes('Package::Name');
This will take $self's built attributes, and uses them in a hash_ref to create a new object instance of 'Package::Name'.
It does check the init_arg of the attribute in $self, and uses that as the key for the value. If you have a Ref in the attribute, then it will be the same Ref in the new object. (So, in this case, it isn't strictly cloned, please don't be pedantic).
If you wish to pass through additional information, or use a different value for an attribute that you already have a value for in the current object, then a hash_ref can also be provided with key/value pairs so that these can be used/added as as well
my $object_with_attributes = $self->new_with_cloned_attributes('Package::Name',{
attr1 => q{val1},...
});
There are also two additional methods provided by this role, 'attributes_as_json' and attributes_as_escaped_json'. With these methods, you can get a JSON string with the values of all built attributes, which you could then use elsewhere, such as just building a data hash or pushing into a commandline. More info can be found in the Documentation, but it is worth noting, objects are not stringified, throughout the whole data structure. In the case where this would be in an array, a null will be found instead.
I hope that this will be found to be useful. Please give me any feedback for this that you wish.
Sunday, 8 November 2009
Friday, 6 November 2009
Extending a Role attribute
Not so much a blog post, more a blog question.
When you are 'extend'ing a Moose base class, Attributes can be extended
package Super::Hero;
use Moose;
has q{super_abilitity} => (isa => q{Str}, is => q{rw});
...
package My::Hero;
use Moose;
extends qw{Super::Hero};
has q{+super_abilitity} => (required => 1);
...
However, in this case, I would like Super::Hero to be a Role to be consumed by some class.
package Super::Hero;
use Moose::Role;
has q{super_abilitity} => (isa => q{Str}, is => q{rw});
...
However, the following doesn't work:
package My::Hero;
use Moose;
with qw{Super::Hero};
has q{+super_abilitity} => (required => 1);
...
The required 'extension' is just ignored. I have to actually declare the whole attribute again.
package My::Hero;
use Moose;
with qw{Super::Hero};
has q{super_abilitity} => (isa => q{Str}, is => q{ro}, required => 1);
...
I can't find anything in CPAN Documentation to confirm or deny that this the deliberate design. Does anyone have any suggestions as to any solutions to this?
Any help appreciated.
When you are 'extend'ing a Moose base class, Attributes can be extended
package Super::Hero;
use Moose;
has q{super_abilitity} => (isa => q{Str}, is => q{rw});
...
package My::Hero;
use Moose;
extends qw{Super::Hero};
has q{+super_abilitity} => (required => 1);
...
However, in this case, I would like Super::Hero to be a Role to be consumed by some class.
package Super::Hero;
use Moose::Role;
has q{super_abilitity} => (isa => q{Str}, is => q{rw});
...
However, the following doesn't work:
package My::Hero;
use Moose;
with qw{Super::Hero};
has q{+super_abilitity} => (required => 1);
...
The required 'extension' is just ignored. I have to actually declare the whole attribute again.
package My::Hero;
use Moose;
with qw{Super::Hero};
has q{super_abilitity} => (isa => q{Str}, is => q{ro}, required => 1);
...
I can't find anything in CPAN Documentation to confirm or deny that this the deliberate design. Does anyone have any suggestions as to any solutions to this?
Any help appreciated.
Saturday, 31 October 2009
10 years at The Sanger - Have I finally achieved my career goal
Friday the 30th was my last working day of year 10 at the Sanger Institute. I have worked in 4 teams whilst there, plus I had been there for 2.5 months before officially starting temping in the glasswash team. Also, in January, I hit the momentous age of 35. (Which, according to bible references to 3score and 10 being the life of a man, makes me middle aged).
So I wonder if I have finally reached what I wanted my career to really be. Over the next month, NaNoWriMo (http://www.nanowrimo.org/) is on and I have decided that I might just spend the next month putting to paper my life so far. I don't claim it will be great (I'm fairly average in most things), but I think it will be good to recap and look back, coming from a middle class background, who wanted to be a bricklayer when I played with Lego aged 5 and a computer programmer aged 13 (but couldn't find anything at school to help him), to being a teacher, scientist on Human Genome Project and a Perl developer.
Who knows, maybe it will interest people, maybe it won't. But a life recap can't be bad.
And maybe it will just make me think, life really isn't too bad, you know.
So I wonder if I have finally reached what I wanted my career to really be. Over the next month, NaNoWriMo (http://www.nanowrimo.org/) is on and I have decided that I might just spend the next month putting to paper my life so far. I don't claim it will be great (I'm fairly average in most things), but I think it will be good to recap and look back, coming from a middle class background, who wanted to be a bricklayer when I played with Lego aged 5 and a computer programmer aged 13 (but couldn't find anything at school to help him), to being a teacher, scientist on Human Genome Project and a Perl developer.
Who knows, maybe it will interest people, maybe it won't. But a life recap can't be bad.
And maybe it will just make me think, life really isn't too bad, you know.
Sunday, 25 October 2009
Course on Moose and Catalyst
I work at the Wellcome Trust Sanger Institute. In my team, New Pipeline Development, we have identified a need for some training in Moose and Catalyst. If anyone has any course recommendations, I'd be interested in hearing about them to propose to my team and training co-ordinator.
The course can be aimed at people who are experienced Perl developers, but only have a small amount of experience of Moose and Catalyst (as anyone who reads my blog will probably identify).
Replies as Comments, or emails will be appreciated. Many Thanks in advance.
The course can be aimed at people who are experienced Perl developers, but only have a small amount of experience of Moose and Catalyst (as anyone who reads my blog will probably identify).
Replies as Comments, or emails will be appreciated. Many Thanks in advance.
Wednesday, 21 October 2009
Does anyone else do this?
I've been away on holiday, and I'm just going back over some comments left on my blog, and got this one?
"Is that a new fashion to use q{string} instead of 'string'?"
My response:
"Just a habit I have gotten into since using perl::critic and PBP.
As '' and ' ' are not allowed by 'the rulez' and you should use q{} and q{ }, (and their double quote equivalents}, I have the coding habit of just using then straight off."
Anyone else doing this as a matter of course, or is it just me? I notice going through a number of recent books, ' and " are used in most of the code examples.
"Is that a new fashion to use q{string} instead of 'string'?"
My response:
"Just a habit I have gotten into since using perl::critic and PBP.
As '' and ' ' are not allowed by 'the rulez' and you should use q{} and q{ }, (and their double quote equivalents}, I have the coding habit of just using then straight off."
Anyone else doing this as a matter of course, or is it just me? I notice going through a number of recent books, ' and " are used in most of the code examples.
Monday, 12 October 2009
How to put your perl ironman pic on blogspot
Go to your dashboard, and under the chosen blog, select layout
Under Add and arrange page elements, select one of the two add a gadget depending if you want it on your sidebar, or at the bottom.
Select the html/javascript gadget.
Give it a title.
In content type a html img tag containing the following
http://ironman.enlightenedperl.org/munger/mybadge/male/setitesuk.png
replacing setitesuk with your username or nickname (male can be switched to female).
click save and hey presto, it will now appear the next time you load your blog.
Note: I am sure I am duplicating this post from somewhere. Partly, this is for my own info, partly to help anyone who also can't find the details easily (apart from mst's blog about the url for the image.)
Under Add and arrange page elements, select one of the two add a gadget depending if you want it on your sidebar, or at the bottom.
Select the html/javascript gadget.
Give it a title.
In content type a html img tag containing the following
http://ironman.enlightenedperl.org/munger/mybadge/male/setitesuk.png
replacing setitesuk with your username or nickname (male can be switched to female).
click save and hey presto, it will now appear the next time you load your blog.
Note: I am sure I am duplicating this post from somewhere. Partly, this is for my own info, partly to help anyone who also can't find the details easily (apart from mst's blog about the url for the image.)
Sunday, 11 October 2009
Is it easier to be specific?
I'm re-assessing some code, trying to refactor it into a re-usable role. Simple, you might think? But is it?
It is very easy to write your code to perform a task on a specific item, or in a specific way. From variable names which mean something tangible, to methods designed to act on a pathway which is unique to your production setup. But, how do you make it more usable?
1 - Variable-Method names
I was taught to make my variable/method names mean something. This makes the code more readable.
$donut = q{jam donut};
...
eat($donut);
instead of
$d = q{jam donut};
...
eat($d);
This is a trivial example, but the principle is there.
However, you (read I) can take it too far. One such point is in directories. For the analysis pipeline, we end up with a directory, after a step called GERALD, called GERALD-date.
In the code, we put this into $gerald_dir. Sounds reasonable. Everywhere I read $gerald_dir, I know exactly what it represents.
However, here is the problem. What happens when the step and directory are renamed Harold. Whilst the principle is the same, and the same files are there, suddenly the variable name is wrong. Just grepping the filesystem won't find something like Gerald. At this point you are probably screaming at me, give it a semantic name, and document what it represents.
Exactly, but that is easy with internal local variable names, not so with public exposed method names. Suddenly I need the role to include a deprecation cycle for the replacement method names, aaahhh!
2 - Application/Locally specific
Anyone should argue that locally specific logic should exist as far up as possible, leaving it out of the generic process end logic as much as possible. No arguements there.
But how do you determine which is app specific, and which is generic.
Obviously, naming conventions are app specific. Or are they? Many things might need to know how to construct a filename in a particular way.
Ok, then how about directory structure? Again, you may find many apps wanting to access the Recalibrated data dir. They all need to know how to do get there.
This is, as you can see, quite a grey area. One where, as I am finding, I think the refactor into a generic role still needs to be a little more specific than might be first thought. You can't get to a directory without at least some knowledge of where it is likely to be. You can't open a file without at least some knowledge of how it is named.
Determine a row in a database - Some way of constructing a query to get it...
Paul Weller said "No one ever said it was gonna be easy", and I wouldnt want it any other way, but remember, if you want your code to be reusable, try to make names semantic, but not specific, and document what they represent.
Also, ensure you document why you process with particular assumptions. At least then, no-one can say you didn't tell them.
That is my plan at least (where I can) from now on.
It is very easy to write your code to perform a task on a specific item, or in a specific way. From variable names which mean something tangible, to methods designed to act on a pathway which is unique to your production setup. But, how do you make it more usable?
1 - Variable-Method names
I was taught to make my variable/method names mean something. This makes the code more readable.
$donut = q{jam donut};
...
eat($donut);
instead of
$d = q{jam donut};
...
eat($d);
This is a trivial example, but the principle is there.
However, you (read I) can take it too far. One such point is in directories. For the analysis pipeline, we end up with a directory, after a step called GERALD, called GERALD-date.
In the code, we put this into $gerald_dir. Sounds reasonable. Everywhere I read $gerald_dir, I know exactly what it represents.
However, here is the problem. What happens when the step and directory are renamed Harold. Whilst the principle is the same, and the same files are there, suddenly the variable name is wrong. Just grepping the filesystem won't find something like Gerald. At this point you are probably screaming at me, give it a semantic name, and document what it represents.
Exactly, but that is easy with internal local variable names, not so with public exposed method names. Suddenly I need the role to include a deprecation cycle for the replacement method names, aaahhh!
2 - Application/Locally specific
Anyone should argue that locally specific logic should exist as far up as possible, leaving it out of the generic process end logic as much as possible. No arguements there.
But how do you determine which is app specific, and which is generic.
Obviously, naming conventions are app specific. Or are they? Many things might need to know how to construct a filename in a particular way.
Ok, then how about directory structure? Again, you may find many apps wanting to access the Recalibrated data dir. They all need to know how to do get there.
This is, as you can see, quite a grey area. One where, as I am finding, I think the refactor into a generic role still needs to be a little more specific than might be first thought. You can't get to a directory without at least some knowledge of where it is likely to be. You can't open a file without at least some knowledge of how it is named.
Determine a row in a database - Some way of constructing a query to get it...
Paul Weller said "No one ever said it was gonna be easy", and I wouldnt want it any other way, but remember, if you want your code to be reusable, try to make names semantic, but not specific, and document what they represent.
Also, ensure you document why you process with particular assumptions. At least then, no-one can say you didn't tell them.
That is my plan at least (where I can) from now on.
Subscribe to:
Posts (Atom)
