develooper Front page | perl.perl5.porters | Postings from January 2014

Inconsistent prototype errors.

Thread Next
From:
James Raspass
Date:
January 30, 2014 21:41
Subject:
Inconsistent prototype errors.
Message ID:
CAP4Ky=93peQT7-kjhfFZCbfhWKjqDdNvpowqe+yxJhajUXPjEw@mail.gmail.com
Errors from oversupplying prototyped subroutines seems to behave
differently depending on the prototype. For example take the following
simple script:

sub eat($){}

my @foo = ( eat 1, 2, 3 );

print "$_\n" for @foo;

That works as expected and prints 2 then 3. However if one changes the
prototype to accept two scalars like so:

sub eat($$){}

Rather than printing just 3, like I (naively) expected, the program refuses
to compile with the following error:

Too many arguments for main::eat

So I guess my question is, if errors from oversupplying prototyped
subroutines is desirable, why are single scalar accepting subroutines
exempt?

To give a little purpose to this question, I was trying to create an
subroutine that spits out enums and wanted to create them in list context
with minimum line noise like so:

my %foo = (
    bar => enum bar => 123,
    baz => enum baz => 456,
);

This was all tested on Perl 5.19.7, I can try other Perls but I wanted to
ascertain whether the behaviour is intentional first. Also hope this wasn't
too long winded, this is my first P5P message.


James Raspass

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About