Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Is there any difference between prototype % and @ ?

by LanX (Bishop)
on Feb 22, 2013 at 13:57 UTC ( #1020166=perlquestion: print w/replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:


in Re: Is there a simple syntax to logically slice a hash? I used a '%' in the prototype of a grep like function

sub hgrep (&%)

I somehow expected it to better than sub hgrep (&@) by warning if the number of passed list elements is odd.

But neither from tests nor documentation can I find any differences...

Is it just cosmetics?

Cheers Rolf

UPDATE: of course I'm not talking about the documented behavior for \% or \[%]

Replies are listed 'Best First'.
Re: Is there any difference between prototype % and @ ?
by Athanasius (Chancellor) on Feb 22, 2013 at 14:07 UTC

    From Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen, under the heading “Problems with Regular Prototypes”:

    Since we’re having so much fun, let’s move on to “%”. This “prototype” means what? That we’re expecting a hash? Not at all! In fact, it is completely identical to a “prototype” of just “@”. Everything I said about “@” is true for “%”, because they are the same! You can’t get any type checking here. It doesn’t even bother to check whether you have an even number of arguments.

    Not official documentation, exactly, but — well, the author did co-write the Camel Book ;-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      > Hope that helps,

      thanks, it does, though it was what I expected it helps to know the official truth...

      > It doesn't even bother to check whether you have an even number of arguments.

      which is - mildly expressed - "unfortunate". I will add a manual check.

      Cheers Rolf

        ... an even number of arguments...
        I will add a manual check.

        What are you doing with the arguments subsumed under the  % prototype? Well, right now you're probably just writing test code to check the behavior of the prototype, but in a 'real world' case, would you not assign these arguments to a hash? What else? If assigning an odd number of elements to a hash, Perl warns (if warnings are enabled) with no separate check necessary. Granted, the warning doesn't stem from the prototype checking system, but...

        >perl -wMstrict -le "sub H (&%) { my ($cr, %h) = @_; $cr->(%h); } ;; H { print @_; } qw(a 1 b); " Odd number of elements in hash assignment at -e line 1. Use of uninitialized value $_[3] in print at -e line 1. a1b
Re: Is there any difference between prototype % and @ ?
by BrowserUk (Pope) on Feb 22, 2013 at 14:12 UTC

    It does make a difference when prefixed with \. Eg. sub foo(&\%) {}.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1020166]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-07-15 23:37 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (328 votes). Check out past polls.