[vox-tech] subroutine arguments with PERL

Matthew Van Gundy matt-lugod at shekinahstudios.com
Tue Nov 30 11:53:51 PST 2010


On 11/30/10 10:59 AM, Brian Lavender wrote:
> How do you tell perl to warn or not compile if you are not using
> the correct number of arguments in a subroutine?
>
> sub foo() {
>    my ($stat_ref, $record_number, $prev_line, $line, $line_number) = @_;
>    # do stuff
> }
>
> # say in my call, I put the wrong number of arguments.
>
> &foo($stat_ref, $record_number, $prev_line, $line, $line_number);

Remove the ampersand before foo...
foo($stat_ref, $record_number, $prev_line, $line, $line_number);

On Perl 5.10.1 that leads to the following error message:
Too many arguments for main::foo at /tmp/args.pl line 10, near 
"$line_number)"

If you put parenthesis after the subroutine name, you are telling Perl 
to check the type and number of arguments.  E.g. sub noargs() { }, sub 
onescalar($) { }, etc.

Cheers,
Matt



More information about the vox-tech mailing list