John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Consider a mild-mannored function that takes an input and returns an output. Should I write that function such that if it takes a bunch of arguments, should do each one and return a list of answers? That's what chom?p does, as do a few others. But many, such as cos, don't.

AFAIK, the caller can easily call map himself. But when does it make sense to implement it that way?

  • Comment on Should we handle multiple arguments as a list?

Replies are listed 'Best First'.
Re: Should we handle multiple arguments as a list?
by aquacade (Scribe) on Jul 22, 2001 at 05:30 UTC

    If you're asking how to write a function that can process either a scalar or a list of values (operating on each value in the list in turn) and the function returns EITHER a scalar OR a list see the Camel book for 'wantarray' function. Here's a quick example for you:

    # Trim beginning and trailing whitespace only sub trim { my @out=@_; # reassign so we don't change input values for (@out) { # loop and substitution are using default system variable $_ s/^\s+//; s/\s+$//; } # the return uses Perl's only trinary operator # COND ? THEN : ELSE # Was this subroutine called in list context or not? return wantarray ? @out : $out[0]; } # main program my @list=(' List 1 ',' List 2 ',' List 3 '); my $val=' Scalar '; print "'$_'\n" foreach (trim($val)); print "'$_'\n" foreach (trim(@list)); __END__

    Hope this answers your question.

      Even more fun (but less efficient) than this would be to just make the function recursive.

      sub add_two { my @in_values = @_; if( wantarray ) { #must use +0 to force scalar #otherwise we get deep recursion return map add_two( $_ ) + 0, @in_values; } else { return $in_value[0] + 2; } }
Re: Should we handle multiple arguments as a list?
by voyager (Friar) on Jul 22, 2001 at 05:14 UTC
    How about when the function typically takes a single argument and returns a formatted (v. calculated) version. When you are formatting (lc, chomp, etc) you are very likely to have a list you want to operate on. When you are calculating, it is less likely.

    Thinking as I type, I guess any function that takes one argument and returns one value, *could* return a list if passed a list *and* called in list context. (>1 argument not called in list context might cause a warn/die).

Re: Should we handle multiple arguments as a list?
by CharlesClarkson (Curate) on Jul 22, 2001 at 09:21 UTC

    I think this involves the communication skills of a programmer. Who is using this function? Is it it usually called many times or just once or twice. During the past week I was helping a newbie with subs to process partial log files. The sort functions lent themselves to speedy list processing, but users would rarely trigger such calls and we used a loop for multiple calls to increase efficiency of the sub.

    It goes back to: "know your customers" or in this case your typical users.


    HTH,
    Charles K. Clarkson