Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Are prototypes evil?

by demerphq (Chancellor)
on Sep 03, 2002 at 11:19 UTC ( #194729=note: print w/ replies, xml ) Need Help??


in reply to Are prototypes evil?

Prototypes are like guns and nuclear power. Not necessarily evil per se, but so inclined to abuse that to consider them not evil would be to underestimate a very dangerous thing.

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.

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!
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 funky(&$) { my $sub=shift; my $string=shift; $sub->() foreach split //,$string; } funky {print $_} "yabbadabbadoo";
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).

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)


Comment on Re: Are prototypes evil?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://194729]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-07-13 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (251 votes), past polls