http://www.perlmonks.org?node_id=299098


in reply to Re: Re: Re: Re: Short routines matter more in OO?
in thread Short routines matter more in OO?

Why are you assigning to $self->{"property$n"} and then returning $self->{property}?

Your @_ test also makes little sense to me. If you are trying to write get/set routines, you would want to get on a single routine, set on 2, and think about throwing an error on 3 or more. Your methods as written require an extra argument to say, "set, pretty please".

I was also going to chide you for having methods called property1, property2 without thinking through what you are going to do if you had more than 2 properties, and then I realized that you were likely talking properties of an object, not a computer representation of physical properties. :-)

Oh, right. If you are going to have a lot of very similar code, then I think that autogenerating can be good. (Assuming, of course, that the person who will take over the code is capable of figuring out autogenerated code...) For instance:

foreach my $accessor (@object_properties) { no strict 'refs'; *$accessor = sub { my $self = shift; $self->{$accessor} = shift if @_; $self->{$accessor}; }; }
(Yeah, yeah. A million CPAN modules implement variations on the above for you.)