Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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)

In reply to Re: Are prototypes evil? by demerphq
in thread Are prototypes evil? by Django

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2018-02-21 21:39 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (288 votes). Check out past polls.