Your first example shows a limitation with Perl. By being forced to use a conditional to deal with the number of arguments, we automatically introduce more chances for bugs because every conditional is an opportunity for bugs to arise. The fewer conditionals you have, the fewer bugs you're likely to have. Obviously you can't avoid them, but fewer should be needed with proper OO design. With Java, the method called is automatically the one you want. Not so with Perl. What happens when you want to handle one, two, or three arguments with different behavior if the optional third argument is a string or a float? In Java, that's four methods. In Perl, typically it's an ugly if/else construct with the logic handled in the method or then dispatched to to the other four methods.
As for the case of foo(@bar), Java wouldn't handle that the way a Perl programmer is expecting because it doesn't turn that array into an argument list. Instead, it would dispatch it to a method that takes an Array object. To simulate that in Perl, create an array object or pass the array by reference.
This also brings up a strength of Perl that's a limitation in Java and many other languages. In Perl, are functions and methods are implicitly variadic (they take a variable number of arguments). If you code your methods right, this can be useful, but it can also mean that arguments fall off the end.
Because of Perl's behavior, strange bugs are available. Consider this:
sub foo { return @foo } $object->bar($wibble, foo());
Some people might be fooled into thinking they're passing at least two arguments. However, if @foo is empty, then &foo will return an empty list and when the list is flattened into the argument list of &bar, you'll only have one argument:
$ perl -MData::Dumper -e 'sub foo {@a}; bar(1,foo()); sub bar {print D +umper \@_}' $VAR1 = [ 1 ];
Again, I love Perl, but the lack of useful prototypes can really hamper the language at times.
Cheers,
Ovid
New address of my CGI Course.
In reply to Re: Re: Re: perl6 & OO
by Ovid
in thread perl6 & OO
by chance
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |