Re: Function overloading in perl
by NetWallah (Canon) on Mar 30, 2009 at 05:12 UTC
|
The term "overload" is overloaded.
In perl, function overloading in the traditional sense - i.e. the same function works differently depending on context and parameters - is not perlish.
The distinguishing factor is that function parameters in perl are always @_, so there is no real way to do something different (other than "wantarray"), to implement traditional overloading.
Nor, do we feel there is any need. Could you explain what you are trying to do, so we can present perlish alternatives ?
..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.
| [reply] |
Re: Function overloading in perl
by tilly (Archbishop) on Mar 30, 2009 at 08:16 UTC
|
Perl 5 doesn't implement function overloading in the sense that I think you are talking about. If you wish to achieve the same effect it is up to you to make your function examine the parameter list and then do what you want. Usually this is just a matter of supplying default values to missing arguments. But if you want you can look at the parameters then redispatch the request to other functions.
If your dispatching logic will be complicated then you may wish to take advantage of the fact that method dispatch on objects lets you easily dispatch on the type of your first argument. | [reply] |
Re: Function overloading in perl
by ikegami (Patriarch) on Mar 30, 2009 at 05:06 UTC
|
Just create another function with the same name (in the same package). no warnings 'redefine'; will remove the warning. (An answer light on details for a question light on details...)
| [reply] [d/l] |
Re: Function overloading in perl
by cdarke (Prior) on Mar 30, 2009 at 07:42 UTC
|
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. | [reply] |
|
|
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.
| [reply] |
|
|
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.
| [reply] |
|
|
Re: Function overloading in perl
by locked_user sundialsvc4 (Abbot) on Mar 30, 2009 at 13:01 UTC
|
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.
| |
|
|
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).
| [reply] [d/l] [select] |
Re: Function overloading in perl
by gokuraku (Monk) on Apr 01, 2009 at 19:25 UTC
|
I've done this when I have written some OO Perl (in 5.8) to be able to use the same functions across different platforms but with different parameters (such as Windows and Unix naming conventions). In that case we did it pretty much in the same way as any other OO library, where the parent might define a function as empty or die if its not in the child so we could make sure that the proper libraries were being loaded and used.
Depends on what you are doing, and how.
| [reply] |
Re: Function overloading in perl
by Anonymous Monk on Sep 07, 2010 at 15:25 UTC
|
Losely typed languages (like Perl).
Have a difficult time implementing overloading.
If I had this code:
my $a = 1;
print OVERLOADED;
sub OVERLOADED{
#This sub wants a char array
return (shift eq "one"?1:0);
}
sub OVERLOADED{
#This sub is illegal in Perl
#This sub wants an int
return (shift == 1?1:0);
}
Note that the very things that I would use to define overloading are not avalible. The same thing goes for lists. | [reply] [d/l] |
|
|
In perl, you have to do things the perl way, which means you can only have sub OVERLOADED
sub OVERLOADED {
## do different things depending on arguments
}
you can't blindly copy the c/c++ concept of function signatures/overloading and expect it to work | [reply] [d/l] |