http://www.perlmonks.org?node_id=406259


in reply to Gratuitous use of Perl Prototypes

I completely agree with the {sentiment embodied in} the title of your meditation.

However, I think that the word "gratuitous" is important. If prototypes were truely evil, as opposed to just widely misunderstood (Poor things. All say "Ahhh!"), then I think they would have been officially deprecated long ago.

But they haven't. Maybe, because they do allow some things to be done that cannot be done any other way. And whilst you may question the necessity of doing those things, the desirability of them, when they are justified, is enough to keep them included in the langauge.

Example: Remove the prototype from this implementation of zip and then modify the code to compensate and you get a much less readable and useable function.

#! perl -slw use strict; local $, = ' | '; sub zip (&\@\@) { map { $_[ 0 ]->( $_[ 1 ][ $_ ], $_[ 2 ][ $_ ] ) } 0 .. ( @{$_[ 1 ]} < @{$_[ 2 ]} ? $#{ $_[ 1 ] } : $#{ $_[ 2 ] } ); } my @a = 'a' .. 'z'; my @n = 1 .. 10; print zip{ "@_" } @a, @n; print zip{ $_[ 0 ] .= $_[ 1 ] } @a, @n; print zip{ $_[ 0 ] .= $_[ 1 ] } @a, @n; __END__ [ 1:37:32.71] P:\test>406231 a 1 | b 2 | c 3 | d 4 | e 5 | f 6 | g 7 | h 8 | i 9 | j 10 a1 | b2 | c3 | d4 | e5 | f6 | g7 | h8 | i9 | j10 a11 | b22 | c33 | d44 | e55 | f66 | g77 | h88 | i99 | j1010

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Gratuitous use of Perl Prototypes
by dragonchild (Archbishop) on Nov 09, 2004 at 14:25 UTC
    Example: Remove the prototype from this implementation of zip and then modify the code to compensate and you get a much less readable and useable function.

    I'd argue that zip() isn't that readable to begin with! *grins* Plus, can't you use some variant of reduce() or mapcar() to achieve the same thing?

    Also, isn't the subref prototype prone to memory leaks? I thought that's why we don't use the try/catch implementation in Error ...

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      I'd argue that zip() isn't that readable to begin with! *grins*

      I guess I could rename it '¥' :)

      Or maybe you'd prefer map_one_element_from_each_of_two_arrays_at_a_time()?

      Or should that be map_one_element_at_a_time_from_each_of_two_arrays()?

      Plus, can't you use some variant of reduce() or mapcar() to achieve the same thing?

      Not that I am aware off. Anoyone know a better implementation?

      Also, isn't the subref prototype prone to memory leaks?

      Again. Not that I am aware of. Maybe the problem lies in the module?


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        *snickers* It wasn't the name I was complaining about - it was your complete lack of variable names. :-)

        And, now that I'm thinking about it, your version isn't prone to memory leaks because you don't have any lexical variables (which may be a good reason for writing it the way you did). It was the use of lexicals that, potentially, could create memory leaks in Error.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.