stevieb has asked for the wisdom of the Perl Monks concerning the following question:
Hey all,
While I'm working with fellow Monk bobf on a module problem, I got looking at one of my own, and would like some insight.
Essentially, a user calls one of my functions (or methods, if they are using the OO interface) as such:
my @array = Pack::has->(...); my @array = Pack::missing->(...);
I'd like to keep the current interface, but merge the two subs into one due to them being so similar (code snip below). To do so, I'd need to change the value of the 'want_what' hash key to the name of the function that was called. For that aspect, I feel I can use caller(), but wonder if there is a more idiomatic approach. Also, the module itself will need to differentiate between has() and missing() if I was to consolidate them into generic(), particularly for legacy users... is there a decent approach to do this, or is changing the interface the Right Thing to do looking forward?
My second question relates to the exists||'' line. Can that be shortened, or does it truly depend what is in the var being tested?
sub has { my $self = shift; my $p = shift; if ( ! exists $p->{ search } or $p->{ search } eq '' ){ return (); } $p->{ want_what } = 1; return @{ _get( $p ) }; } sub missing { my $self = shift; my $p = shift; if ( ! exists $p->{ search } or $p->{ search } eq '' ){ return (); } $p->{ want_what } = 0; return @{ _get( $p ) }; }
I'll gladly clarify or elaborate if necessary.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Combining two subs within a module (SMoP)
by tye (Sage) on Apr 29, 2012 at 02:45 UTC | |
by stevieb (Canon) on Apr 29, 2012 at 02:54 UTC |