I have found myself writing a lot of little information transformation functions lately, for instance, we have a DB table that stores County names and state assigned County_ID's. So i have a function called county_name_by_id() that takes a db_handle, and a county_id as input, and returns the county name.

There are often occasions where i'd like to look up multiple county names from their ids at one time, so I allow the second param, county_id to be either a scalar (one county_id), or an arrayref of county_ids.

If called with one county id, it returns the name as a scalar, and if called with an arrayref, it returns a hashref where the keys are the county_id's and the values are the corresponding names.

This works fine, but I am determining the return context based on the input params, as opposed to using wantarray(). So this seems a bit paradoxical, because if i call the function with 1 county_id, I know I want 1 name back, and calling with multiple county_id's, I want multiple names back.

In any case, wantarray() wont tell me anything useful, becuase I'm returning either a string or a hashref, which get assigned to a scalar, so wantarray() will return defined but false in both cases if I call it with a proper return assignment:

my $name = county_name_by_id($dbh, 23); my $name_refs = county_name_by_id($dbh, [12, 25, 7, 13]);
So, is this type of call / return pairing a normal thing to do, or am I smoking crack today?

In reply to function call / return philosophies by shemp

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.