KK99
KK99

Reputation: 1989

Function returning 2 types based on input in Perl. Is this a good approach?

i have designed a function which can return 2 different types based on the input parameters

ex: &Foo(12,"count")   -> returns record count from DB for value 12  
    &Foo(12,"details") -> returns resultset from DB for value 12 in hash format

My question is is this a good approach? in C# i can do it with function overload.

Upvotes: 1

Views: 84

Answers (2)

hpavc
hpavc

Reputation: 1335

additionally you might consider the want to make foo return the details if you wanted a list.

return wantarray ? ($num, @details) : $num;

Upvotes: 1

Thilo
Thilo

Reputation: 262714

Please think what part of your code gets easier by saying

Foo(12, "count")

instead of

Foo_count(12)

The only case I can think of is when the function name ("count") itself is input data. And even then do you probably want to perform some validation on that, maybe by means of a function table lookup.

Unless this is for an intermediate layer that just takes a command name and passes it on, I'd go with two separate functions.

Also, the implementation of the Foo function would look at the command name and then just split into a private function for every command anyway, right?

Upvotes: 4

Related Questions