in reply to Function overloading in perl

Remember that (in one of several “ways to do it”), the arguments to any function-call are simply ... a list. Therefore, it is indeed possible for the number of arguments and so-forth to vary from one call to the next.

It would be possible, albeit somewhat cumbersome, for a sub to examine its parameter-list and “do the right thing.”

As guidance, I would say:   “whatever you decide to do, make whatever you decide to do abundantly clear.” Personally, I do not find “overloaded functions” to meet that qualification, so I don't use them. But, that's just me. Keep in mind both yourself and all those who will follow you... the latter of which, of course, cannot divine your intentions (as you may recal, you got smooshed by a bread-truck, poor sot...) and have only your code to go by.

Replies are listed 'Best First'.
Re^2: Function overloading in perl
by moritz (Cardinal) on Mar 30, 2009 at 13:16 UTC
    This is getting a little off-topic now, but let me add something about Perl 6 here.

    In Perl 6 subs and methods have real signatures, and multi subs and methods (what we call "overloaded" in this thread) are a fundamental mechanism to extend the language.

    One example (straight from S13) is the case where you want to add language dependent case conversion rules. Instead of hacking in locales, you'd just define a type TurkishStr, and a sub multi sub uc(TurkisStr $str) { ... } converts a small i with dot to İ (capital letter I with dot above). Then uc($str) will do what I mean, in all cases.

    Presumably TurkishStr inherits from the normal Str class, so that all other operations (like substr, index etc) remain unchanged for that type.

    Since multi dispatch is a rather fundamental for extending the language, it is very sophisticated and takes into account arity, types, subset types and type breakers (like type narrowness and default fallbacks).