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;
}

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

Reply via email to