in reply to Function overloading in perl

Traditional function overloading in a language like, say, C++, is provided so that a different function can be called depending on the parameter list signature. C++ has a formal parameter list for its fuctions, Perl does not so it is not part of its culture. You might like to consider if it is wise to try to bring a culture from one language into another.

Personally I have never been convinced about the advisability of function overloading, my view is that if you need different functionality then you should call a different function.

Replies are listed 'Best First'.
Re^2: Function overloading in perl
by tilly (Archbishop) on Mar 30, 2009 at 08:07 UTC
    What if you need the same functionality but it would be convenient to make some parameters be optional? In C++ you'd do that with function overloading. In Perl you'd do that while processing your arguments. The existence of a different way to do it in Perl doesn't make function overloading the wrong thing to do in strongly typed languages such as C++ and Java.
      if you want some values to be optional, then make them optional. All depends on how you put the data in, and how you take it out; easy enough to null-pad, or whatever is relevant to the data type.
        If your language says that foo(bar, baz) is defined but foo(bar) is not, then you're unlikely to win the argument with your language. Sometimes you have to work within the rules, not make them.

        (Googles.)

        My initial response was not quite right, you can have optional arguments in C++ after all. But you can't in Java because the language doesn't support having optional arguments. There to get the same effect you really do have to do function overloading. The overloaded function is pretty easy to write (it just calls foo(bar, baz) with a default value supplied), but you still need to write it.