in reply to Are prototypes evil?
Many people _try_ to make prototypes do things that they are _not_ meant to be used for. Such as argument checking. A classic example would be a programmer who writes a function that takes three arguments. So they stick a ($$$) prototype on the sub thinking that now their sub will be compile time checked for recieving three scalars as arguments. Well, it will be checked that it recieves three objects, but it doesnt check that they are scalars, rather it silently coerces whatever it receives into scalars. This means it doesnt behave the way things normally should behave. Furthermore if for some reason i have the parameters I want to pass in an array, I can't just use the array and expect it to be listified into the arguments as normal, rather perl will coerce it into a scalar (the arrays count) and then complain about the missing arguments. All of this is contrary to the behaviour of normal subroutines. While this contraryness will be figured out (immediatley to eventually depending on the perl skill levels involved) it will take time and frustration.
Basically perl is full of exceptions to rules, and exceptions to exceptions to the rules. In many ways this makes perl all the more powerful and interesting. However when completely unusual exceptions to the rules are stuck into code, with apparently no good reason (most times there are better ways to do what people think they are doing with prototypes) it just makes everyones life all the more difficult.
About the only time that I can see prototypes making sense is when the behaviour of a sub should mimic the behaviour of a perl builtin, or when you want to make a "map-like sub":sub foo ($$$) { return "A: $_[0] B: $_[1] C: $_[2]\n"; } my @arg1=("Foo"); my @arg2=("Bar"); my @arg3=("Baz"); print foo(@arg1,@arg2,@arg3); # Ack whats this? # A: 1 B: 1 C: 1 __END__ push @arg1,@arg2,@arg3; print foo(@arg1); # whatdoyoumeanthisdoesntwork!
Also note that prototypes dont work with method calls, nor do they apply in a number of special cases (exceptions that while I know they exist, I cant remember the details of).sub funky(&$) { my $sub=shift; my $string=shift; $sub->() foreach split //,$string; } funky {print $_} "yabbadabbadoo";
Incidentally I wrote a node about all of this some time back: When to use Prototypes? (the answer: almost never)
Yves / DeMerphq
---
Software Engineering is Programming when you can't. -- E. W. Dijkstra (RIP)
|
|---|