in reply to Moo From Hell #1: How to proxy a sub from a non OO module [SOLVED]

(This is unrelated to your actual question / problem.)

Why do you use wantarray?

sub my_uniq { my $self = shift; my @result = uniq @{ $self->list }; wantarray ? @result : scalar @result; }

Why don't you simply write

sub my_uniq { my $self = shift; return uniq @{ $self->list }; }

My guess is it's a performance optimization since it reimplements already existing behaviour. Have you benchmarked it?

My personal opinion - feel free to ignore it.

As far as I can see the method my_uniq is simply a convenience function to make it easier to work with the array reference stored in the field 'list'. I would design the class differently and not provide any direct access to the field 'list' at all. Instead provide an accessor which returns the list as an array and drop the convenience function 'my_uniq'.

# class method sub get_list { my $self = shift; return @{ $self->list() }; }
# client code - variant a # (client wants values and count) my @uniq_list = uniq $obj->get_list(); my $uniq_count = scalar @uniq_list; # client code - variant b # (client just wants the count) my $uniq_count = scalar uniq $obj->get_list();

Main reason: If the user wants to do something else with the list (e.g. reverse) then I don't need to reimplement these functions too.

Replies are listed 'Best First'.
Re^2: Moo From Hell #1: How to proxy a sub from a non OO module [SOLVED]
by AnomalousMonk (Archbishop) on Aug 03, 2015 at 11:52 UTC
    This is unrelated to your actual question / problem.

    But this reply bears upon the question of organization or factoring of code, which is closely related to the OPed question. ++ from me.

    My personal opinion - feel free to ignore it.

    The section advising use of a list getter for member data so that normal list processing functions can be used on the data makes me wish I could ++ again. Ignore this advice if you will, but you do so at your own peril!


    Give a man a fish:  <%-(-(-(-<