Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Suggestions on Object Oriented helpers for 2017

by stevieb (Canon)
on Feb 04, 2017 at 00:41 UTC ( [id://1181042]=perlquestion: print w/replies, xml ) Need Help??

stevieb has asked for the wisdom of the Perl Monks concerning the following question:

So I'm getting sick of doing things like this:

sub new { my ($class, %args) = @_; $self->_foo($args{foo}); $self->_bar($args{bar}); ... ... ... ... } sub _foo { # essentially these methods are setter/getters with # a bit of sanity checking on the param } sub bar { ... } sub ...

I think I'm finally ready to try out a helper that can expedite the writing of these subs that almost always do the same things. For example, sometimes I do trickery like this:

BEGIN { $param_map = { channel => \%mux, queue => \%queue, polarity => \%polarity, rate => \%rate, mode => \%mode, gain => \%gain, }; no strict 'refs'; for my $sub (keys %$param_map) { *$sub = sub { my ($self, $opt) = @_; if (defined $opt) { if (! exists $param_map->{$sub}{$opt}) { die "$sub param requires an integer\n"; } $self->{$sub} = $param_map->{$sub}{$opt}; } my $default = "DEFAULT_" . uc $sub; my $max = "MAX_" . uc $sub; $self->{$sub} = __PACKAGE__->$default if ! defined $self->{$sub}; $self->_bit_set($self->{$sub}, __PACKAGE__->$max); return $self->{$sub}; } } }

...but that only works when all params are reasonably consistent in the values that are accepted.

I've heard stories about Moose, Mouse, Moo etc., but since I've never gone down this path before, I'm looking for today's take on which one I should focus on. Keeping prereqs low and performance high are very important, as is maintainability, and activity on the distribution chosen.

I don't want my scripts to take seconds to start up, nor do I want CPAN Testers failures every few weeks due to random changes being done that aren't tested properly in these modules, otherwise, I'll just keep doing what I'm doin'.

So, looking for feedback from the Monks who have "been there, done that" as far as considering transitioning from traditional Perl OO to some form of framework to take a bit of the sting out of duplicated code, with these (not limited to, nor in any specific order):

  • performance (especially startup)
  • minimal dependency chain
  • ease of use
  • stable API
  • great documentation
  • availability of author(s)

To be honest, I never thought I'd consider going this route, but I've been writing a lot of distributions lately (several that are 'in the works'), and just want to consider and do some prototyping with such a distribution to see if it'll make my life easier while outweighing the drawbacks.

I'm especially interested in hearing about newer ones that aren't spoken about so much, and I may not know about. What are your stories?

Thanks,

-stevieb

Replies are listed 'Best First'.
Re: Suggestions on Object Oriented helpers for 2017
by Your Mother (Archbishop) on Feb 04, 2017 at 00:46 UTC

    I like Moo very well (mixed with Type::Tiny::Role and various helpers as necessary but it’s often not). Mouse::XS is probably faster and more Moose compatible. I don’t find myself missing anything when I don’t use Moose/Mouse.

      Thanks Your Mother. The more I'm reading, I'm liking Moo as well. Looks very compatible and a very well taken care of dist. I do however like the XS of Mouse::XS for my almost-only-C/XS modules, but I'll have to do some code reviews (and possibly benchmarking/profiling) to see whether the speed benefits of XS will outweigh me keeping everything standardized on a single choice.

      To further, I'm still extremely happy with doing things the old way, so I'm looking for very basic functionality (like creating getters/setters) at this time, without the expansive capabilities I'm seeing with Moose. Not to say I won't start desiring such functionality moving forward, but I still like to stay as core perl in my code where possible, even if it's just so I understand in three years what I was thinking, without having to read external docs to understand bits and pieces.

      update: yeah, Moo seems to be acceptable at load-up times, which for most of my purposes, that's acceptable even if it does cost a few clock ticks. Not only that, the docs are clear (the SYNOPSIS is copy/pastable), and it takes minutes to rewrite existing code to use it. I am already liking the isa feature... write a single sub definition instead of doing the whole defined ? ... : ...; kind of cruft myself. It was already installed on most of my *brew installations due to other dists I've installed, so it's likely to be already installed on the systems I'm targeting my dists at anyhow. /update

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1181042]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-19 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found