2009/7/11 Steve Bertrand <st...@ibctech.ca>:
> I have numerous methods such as the following. They all perform the same
> basic function based on the parameter passed in ($name, $key or nothing).
>
> I've recently moved out the actual data definition (hash) into a
> different 'private' function in order to make things easier to read.
>
> Now, I want to consolidate further by having only one 'public' engine to
> provide the return for multiple different hash variables (such as
> payment_methods, acct_status, plan_names etc) that will each be defined
> in their own private functions, instead of copy/pasting.
>
> In the below method, I already extract the data from an external
> (internal to class) method. What I'd like to do is make that internal
> method call dynamic, based on the incoming parameters, so I can have a
> single return engine that does the same thing in all cases, but the call
> from the method will retrieve the requested data before returning.
>
> Can someone recommend some reading that I can do to hone my ability to
> make more dynamic and reusable functions, that will allow for
> easier-to-remember parameters?
>
> sub payment_methods {
>
>    my $self = shift;
>    my $return_type = shift;
>
>    my $options = $self->_defined_payment_methods();
>
>    # return an array of names if told to
>
>    if (defined $return_type and $return_type eq 'name') {
>
>        my @options;
>        for my $key (keys (%$options)) {
>            push @options, $options->{$key};
>        }
>        return @options;
>    }
>
>    # return an array of keys
>
>    if (defined $return_type and $return_type eq 'key') {
>        my @options = keys (%$options);
>        return @options;
>    }
>    return %$options;
> }
>

I am not entirely sure that I understand your requirements exactly but
I think a dispatch table* might be useful here. It will make the
parsing of options easier on the eye. You might also consider named
parameters* for your argument list.

HTH,
Dp.

[1] http://hop.perl.plover.com/book/pdf/02DispatchTables.pdf
[2] http://perldesignpatterns.com/?NamedArguments

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to