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.


In reply to Combining two subs within a module by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.