Gentlemen! I bring you peace in our time!
package Foo;
use Method::Signatures;
method new (%args) { return bless {%args}, $self; };
method get ($key) { return $self->{$key}; };
method set ($key, $val) { return $self->{$key} = $val; };
Very nice! My wrists thank you
You should stop by #moose and chat, I know mst has been planning on writing a Perl 6-ish Moose wrapper using Devel::Declare (tentatively called Moose::Declare), but has been without the tuits to do it. We also already have some work towards method signature checking with MooseX::Method, which can easily be converted to use a Devel::Declare based syntactic sugar. There has also been some talk of pulling the type system out of Moose so it is easier to use it in non-Moose situations, which could be helpful for this kind of stuff.
- Stevan
Re:innovative
audreyt on 2007-12-27T12:16:53
Mark: You just inspired me to write selfvars.pm, now on CPAN.:-) Re: sefvars.pm
markjugg on 2007-12-27T15:56:42
Wow, Thanks Audrey. I like it.
I wonder if it used "Sub::Exporter", if the "self" scalar name could be easily customized, like it can for "self.pm". However, I don't see any examples in the Sub::Exporter docs of exporting scalars. (Perhaps only because it is recommended against in most cases).Re: sefvars.pm
audreyt on 2007-12-28T12:21:58
No, it doesn't use Sub::Exporter; as of version 0.05, it manages the exporting by itself.You can customize the "self" scalar name with an import line:
Or only import self as $this but not args:use selfvars -self => 'this', -args => 'opts';Or only import self as $self:use selfvars -self => 'this';More suggestions are welcome as well!use selfvars -self;Re:innovative
jplindstrom on 2007-12-27T21:19:17
Excellent! So long Spiffy.Re:innovative
schwern on 2007-12-28T21:34:42
Clever, but performance is awful. Tied variables.:(
Then again, I've found some fascinating bugs in Method::Signatures.
Re:innovative
audreyt on 2007-12-28T22:48:18
True, but although there is technically a 10-fold difference between 1191563/s vs 114688/s per lookup, in practice I wonder how it's going to matter -- even if it's put in a loop that's going to look up $self more than 100000 times, the difference will still be less than one second.:-) Re:innovative
schwern on 2007-12-29T01:04:03
$self gets used an awful lot. It's one of those things that adds a drag to every single aspect of your code. As much as we'd like to, you can't always ignore the constant part of big-O.Re:innovative
hex on 2007-12-29T01:04:18
That's beautiful, Audrey, thank you. One thought - I always pass by reference, that is to say, $vars. If that could make it into a future selfvars.pm as an option, it would be awesome.Re:innovative
audreyt on 2007-12-29T05:08:05
Do you mean that you always pass a single by-name hash reference argument, in a PBP-esque fashion?If so, how about making available a %opts so you can say:$obj->meth({ a => 1, b => 2 });If you wish you can even import selfvars like this, populating named options into %_:sub meth {
$self->foo($opts{a});
}Would that resemble what you want?use selfvars -opts => '_', -self;
sub meth {
$self->foo($_{a});
}Re:innovative
hex on 2007-12-31T05:10:39
0.10 of selfvars very definitely resembles what I want. I dub thee Lady Leet of Awesomeland!
Re:Unstable magic
schwern on 2007-12-28T21:24:49
You'd have to ask Matt Trout for details, I know he has plans to move as much magic as possible out of the C hack and into Perl. At least it's stable with any given release of Perl. It's not like those exactly SNEAK up on us.