[vox-tech] subroutine arguments with PERL

Troy Arnold troy-vox at zenux.net
Wed Dec 1 11:15:16 PST 2010


On Tue, Nov 30, 2010 at 11:53:51AM -0800, Matthew Van Gundy wrote:
> 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?
> 
> 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.

If you do it this way, you should read the prototype section of the perlsub
docs 'perldoc perlsub'.  There are some definite "gotchas" -- most
especially if ones brain has become firmly wired to expect a Perl sub to
turn its arguments into a flat list of scalars.

-t


More information about the vox-tech mailing list