Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46778

Perl: Correct way to use Sub-routine Prototypes [%@$]

sub Function(\[$@%]);

This works on Windows (Perl v5.14.2 on Windows 7), and accepts all 3 kinds of arguments. Hashes, Scalars and Lists.

However, the same prototype on Linux (Perl 5.6.1 on CentOS release 4.5) gives me

Malformed prototype for main::Function: \[%@$].

On both, I am doing use 5.006. And otherwise, the scripts are exactly identical.

Upvotes: 0

Views: 2463

Answers (1)

ikegami
ikegami

Reputation: 386461

That prototype didn't exist back in 5.6.1. It's a recent addition as it was added in 5.14. (That means it's in all supported versions of Perl.)

You can't pass an array or a hash to a sub, only a list of scalars. The prototype is causing the calling code to take a reference and pass that. That's something you can do explicitly by changing

sub Function(\[$@%]) { }
Function($s);
Function(@a);
Function(%h);

to

sub Function { }
Function(\$s)
Function(\@a)
Function(\%h)

Prototypes are generally to be avoided anyway.

Upvotes: 3

Related Questions