[ILUG] perl subroutine params

John Tobin tobinjt at netsoc.tcd.ie
Thu May 9 15:58:12 IST 2002


On Thu, May 09, 2002 at 03:36:03PM +0100, Gavin McCullagh wrote:
> Hi,
> 
> if I write a perl script and in it a subroutine like 
> 
> #!/usr/bin/perl -w
> 
> use diagnostics;
> use strict;
> 
> &some_subroutine("1","2","3");

Remove the &.  This should get you a "main::some_subroutine() called
too early to check prototype" warning.  Moving the call to after the
definition should get you a "Too many arguments for
main::some_subroutine" error.

> 
> sub some_routine ($$$) {
> 	print "$_[0]\t$_[1]\t$_[2]\n";
> }
> 
> 
> It ws my understanding that if I called the subroutinewith anything other
> than three scalar variables, I should get a complaint.  However I tried it
> out with {0,1,2,4} params and it just works on regardless.
> 
> Am I doing something wrong?  Do I need to tell perl to enforce this?

Snipped from perlsub(1):

       time.  The prototype affects only interpretation of new-
       style calls to the function, where new-style is defined as
       not using the "&" character.  In other words, if you call
       it like a built-in function, then it behaves like a built-
       in function.  If you call it like an old-fashioned subrou­
       tine, then it behaves like an old-fashioned subroutine.
       It naturally falls out from this rule that prototypes have
       no influence on subroutine references like "\&foo" or on
       indirect subroutine calls like "&{$subref}" or "$sub­
       ref->()".


> 
> Gavin

-- 
John
"That would preempt a bunch of problems involved in trying to reconstruct
exactly how the Perl 5 parser thinks, which nobody entirely understands."
			Larry Wall, 2001/04/20, perl6-language at perl.org
"Finger to spiritual emptiness underlying everything." -- How a Japanese C
manual referred to a "pointer to void".




More information about the ILUG mailing list