I would probably not do it the way you've done it, from a user point of view.

To keep it simple and not over-implement, I usually design things like these to work as &foo below.

sub foo { my @names = @_; ... ; return @values if wantarray; return $values[0]; }
Even thought the implementation is explicit, I'd leave the behaviour for a "list call" in scalar context undefined in the documentation.

If I'd want to have it as a hash I could do

my @ids = ...; my %foo; @foo{@ids} = foo(@ids);
or use my utility function &zip
sub zip { my ($x, $y) = @_; map { $x->[$_], $y->[$_] } 0 .. max($#$x, $#$y); } my @ids = ... ; my %foo = zip(\@ids => [ foo(@ids) ]);
which is good when you don't want to use an intermediate variable.

A lot of the time I don't care to handle more than one argument though, because I often use map for that

my @ids = ... ; my @foo = map foo($_) => @ids; my %foo = map { $_ => foo($_) } @ids;
But I don't know enough about how you'll use this function in practice to say anything about how you should do in your code. In particular, you might want to handle list calls because the DB access might benefit from that.

Hope I've helped,
ihb

Read argumentation in its context!


In reply to Re: function call / return philosophies by ihb
in thread 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.