Karryanna
Karryanna

Reputation: 117

Perl: Not enough arguments for function

I'm quite a newcomer to Perl and currently trying to understand mentioned error message.

I've got this piece of code (still mostly for testing pourposes)

#!/usr/bin/perl

use strict;
use warnings;

my %info = (autor => "Karryanna", jazyk => "cs");

vec([%info]);

sub vec
{
  my ($hashref) = @_;
  print $hashref->autor . "\n";
}

When I try to run it, it gives this error message

Not enough arguments for vec at test.pl line 8, near "])"

When I tried to google it, I found this site which just made me think that preceding the subroutine call with an ampersand is enough. I tried it and it really did help.

However, when I later on ran into another problem, googling took me to this SO question. The point is that the answer states

You should call subroutines without &, e.g. populate(...), unless you specifically want to override the prototype of the sub. If you don't know what a prototype is, just don't use &.

I've got absolutely no idea what a prototype in Perl context is. So - what's the reality like? Should I just use & or is there any other, perhaps more "clean", solution?

Thanks!

(By the way, if I may ask a little bit OT question -- Should I really define subroutine after calling it? It seems really unnatural to me, however, I've already discovered that reverting the order makes the compiler complain)

Upvotes: 2

Views: 4154

Answers (1)

cdarke
cdarke

Reputation: 44344

vec is a perl built-in function, you are getting a name collision (perldoc -f vec). Call your subroutine something else.

You probably want to call it like this:

mysub(\%info)

In Perl you can call a subroutine either before or after you declare it. The only restrictions on that are when using prototypes, which you probably don't want to do. What errors do you get when you reverse the order ("makes the compiler complain" doesn't help much)? It might be because of the name collision.

Upvotes: 6

Related Questions