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

Inconsistent prototype errors.

Thread Next
James Raspass
January 30, 2014 21:41
Inconsistent prototype errors.
Message ID:
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

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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About