in reply to Detecting Subroutine Input Type
In your solution above, you distinguish based on number of arguments passed, which prevents you from passing an array of one element (which may or may not be just fine for your purpose).
You could instead choose to check the ref type of the parameter: if (ref $_[0] eq "ARRAY") { ... } else { ... } which would prevent you from passing an array whose first element was an array reference. (Variations on this involve UNIVERSAL::isa($_[0], "ARRAY") or calling an isa method on an object or doing an array dereference in an eval block to see if it fails; this subproblem of "what is an array reference" also has no perfect solution.)
Or combine both the check for one element and the check for an array ref.
There are also prototypes; if you promise to not use them as if they were what other languages call prototypes,
All in all, it's best to document what your routine expects and take only that input; do you really need to accept both?
|
|---|