For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > problems with subroutine prototype checking









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problems with subroutine prototype checking
Gavin Bowlby

2006-01-19, 6:58 pm

All:



I'm having problems trying to get Perl function prototype checking
working correctly.



My code looks something like:





main.pl:

use a;





a.pm:

use b;

use c;



b.pm:

sub b_function ($$$) {

}



c.pm:

use d;



d.pm:

b_function($p1);



I'm expecting to get a compilation error for the subroutine call in
d.pm, since it calls "b_function" with a single scalar as a parameter,
instead of the required 3 scalars as parameters.



A trivial test case that looks like:



main.pl:



sub a ($$$) {

}



a($x);



fails as expected with a compilation failure on the wrong number of
parameters passed to "a", but my application that looks like the above
code (although it's much bigger!) does not cause a compilation failure
when the wrong number of parameters are passed.



My .pm files only contain subroutine definitions, they have no package
statements. The lack of a package statement doesn't appear to change the
problem, however for simple cases I've constructed using a set of "use"
statements.



My Perl version info is:



$ perl -v



This is perl, v5.8.7 built for cygwin-thread-multi-64int



Copyright 1987-2005, Larry Wall



Perl may be copied only under the terms of either the Artistic License
or the

GNU General Public License, which may be found in the Perl 5 source kit.



Complete documentation for Perl, including FAQ lists, should be found on

this system using `man perl' or `perldoc perl'. If you have access to
the

Internet, point your browser at http://www.perl.org/, the Perl Home
Page.



I'm running Perl under Cygwin on Windows XP.



Regards,

Gavin Bowlby




Jimmy

2006-01-19, 6:58 pm


----- Original Message -----
From: "Gavin Bowlby" <gavin.bowlby@qlogic.com>
To: <beginners@perl.org>
Sent: Friday, January 20, 2006 7:57 AM
Subject: problems with subroutine prototype checking


b.pm:

# You may want to add this :
package b;
require Exporter;
our @ISA = qw/Exporter;
our @EXPORT qw/b_function/;

if you don't add the above, you 'll have to call the sub by :
b::b_function ($var);


sub b_function ($$$) {

}


1; # Don't forget this at the end of package
or else, the pacakge will fail to use.


HTH,
Jim


Paul Lalli

2006-01-19, 6:58 pm

Gavin Bowlby wrote:
> I'm having problems trying to get Perl function prototype checking
> working correctly.
>
> My code looks something like:


Not good enough. You need to show us an *actual* program, with real
code. Because you pretty obviously haven't diagnosed your problem
correctly. How do I know this? Witness the following, taken
*directly* from your "sample":

$ cat main.pl
#!/usr/bin/perl
use strict;
use warnings;

use a;

$ cat a.pm
use b;
use c;

1;
$ cat b.pm
sub b_function ($$$){
print "called b_function\n";
}

1;

$ cat c.pm
use d;

1;

$ cat d.pm
$p1 = 'arg';
b_function($p1);

1;

$ perl main.pl
Not enough arguments for main::b_function at d.pm line 2, near "$p1)"
Compilation failed in require at c.pm line 1.
BEGIN failed--compilation aborted at c.pm line 1.
Compilation failed in require at a.pm line 2.
BEGIN failed--compilation aborted at a.pm line 2.
Compilation failed in require at main.pl line 5.
BEGIN failed--compilation aborted at main.pl line 5.
$

Show us real code, and we can give you real help.

Please understand this does NOT mean to post your entire 5-file
program. Parse your problem out until you're able to generate a
short-but-complete script which still demonstrates your problem.

Chances are, once you do this, you'll have found your problem. But if
not, post the resulting short-but-complete program here.

Paul Lalli

Paul Lalli

2006-01-19, 6:58 pm

Jimmy wrote:
> # You may want to add this :
> package b;
> require Exporter;
> our @ISA = qw/Exporter;
> our @EXPORT qw/b_function/;
>
> if you don't add the above, you 'll have to call the sub by :
> b::b_function ($var);
>
>
> sub b_function ($$$) {
>
> }
>
>
> 1; # Don't forget this at the end of package
> or else, the pacakge will fail to use.


Please read the OP's post before responding. The OP specifically said
his files do not contain any package statements. Therefore, all
functions are in the main:: package, and Exporter is wholly unneeded.

Paul Lalli

Gavin Bowlby

2006-01-19, 9:55 pm

Jim:

Thank you very much for looking at this!

Will exporting the function cause prototype checking to be done for all
callers?

I probably wasn't clear in my original posting, the problem I'm having
is that the code sample I gave *does* compile successfully, without
errors. I don't need to export anything to get it to compile
successfully.

I want it to *fail* compilation with an error callout about a parameter
count mismatch.

I haven't tried your suggestion yet, but do you think that what you're
suggesting will cause a compilation error to occur? If so, great, that's
what I'm looking for.

Thanks again for your help,
Gavin


-----Original Message-----
From: Jimmy [mailto:perl@reborn.org]=20
Sent: Thursday, January 19, 2006 4:14 PM
To: Gavin Bowlby; beginners@perl.org
Subject: Re: problems with subroutine prototype checking


----- Original Message -----=20
From: "Gavin Bowlby" <gavin.bowlby@qlogic.com>
To: <beginners@perl.org>
Sent: Friday, January 20, 2006 7:57 AM
Subject: problems with subroutine prototype checking


b.pm:

# You may want to add this :
package b;
require Exporter;
our @ISA =3D qw/Exporter;
our @EXPORT qw/b_function/;

if you don't add the above, you 'll have to call the sub by :
b::b_function ($var);


sub b_function ($$$) {

}

=20
1; # Don't forget this at the end of package
or else, the pacakge will fail to use.


HTH,
Jim



usenet@DavidFilmer.com

2006-01-19, 9:55 pm

Gavin Bowlby wrote:
> I'm having problems trying to get Perl function prototype checking
> working correctly.


You might consider not using subroutine prototypes at all. Ever.

See "Perl Best Practices" by Damian Conway, p 194-196 for a clear
discussion of why subroutine prototypes should be avoided (and what you
should do instead).

John Doe

2006-01-20, 6:59 pm

Hello Gavin

Gavin Bowlby am Freitag, 20. Januar 2006 01.43:
[...]
> I probably wasn't clear in my original posting, the problem I'm having
> is that the code sample I gave *does* compile successfully,


I don't think so, because you try to use packages that are not declared.

> without
> errors. I don't need to export anything to get it to compile
> successfully.
>
> I want it to *fail* compilation with an error callout about a parameter
> count mismatch.

[...]

Your code in the first post misses a lot of things, for example the package
declarations, 'use strict', 'use warnings', the usage of the Exporter module
(or calling the sub qualified with the package name) as Jimmy said.

This makes it difficult to help you.

Please post the actual test code you tried (trimmed to the relevant parts).

greetings joe
Paul Lalli

2006-01-20, 6:59 pm

John Doe wrote:
> Hello Gavin
>
> Gavin Bowlby am Freitag, 20. Januar 2006 01.43:
> [...]
>
> I don't think so, because you try to use packages that are not declared.


That statement is non-sensical. If a package isn't declared, there's
no package. The OP very specifically said that he didn't declare any
packages in his .pm files. Therefore, all code is in package main::.
There is nothing in Perl requiring that other files belong to separate
packages.

Paul Lalli

Randal L. Schwartz

2006-01-20, 6:59 pm

>>>>> ""Gavin" == "Gavin Bowlby" <gavin.bowlby@qlogic.com> writes:

"Gavin> I'm having problems trying to get Perl function prototype checking
"Gavin> working correctly.

Don't use prototypes. Ever.

Well, you're permitted to use prototypes once you understand exactly
why I say "Don't use prototypes. Ever.".

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Chas Owens

2006-01-20, 6:59 pm

Can this be modified to "Don't use prototypes until Perl 6"?

On 20 Jan 2006 06:11:28 -0800, Randal L. Schwartz <merlyn@stonehenge.com> w=
rote:
>
> "Gavin> I'm having problems trying to get Perl function prototype checkin=

g
> "Gavin> working correctly.
>
> Don't use prototypes. Ever.
>
> Well, you're permitted to use prototypes once you understand exactly
> why I say "Don't use prototypes. Ever.".
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 00=

95
> <merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl train=

ing!
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

Xavier Noria

2006-01-20, 6:59 pm

On Jan 20, 2006, at 15:11, Randal L. Schwartz wrote:

>
> "Gavin> I'm having problems trying to get Perl function prototype
> checking
> "Gavin> working correctly.
>
> Don't use prototypes. Ever.
>
> Well, you're permitted to use prototypes once you understand exactly
> why I say "Don't use prototypes. Ever.".


Canonical pointer:

FMTEYEWTK about Prototypes in Perl
http://library.n0i.net/programming/.../fm_prototypes/

-- fxn

PS: Does anybody know why this article is no longer available from
Perl.com?


Randal L. Schwartz

2006-01-20, 6:59 pm

>>>>> "Chas" == Chas Owens <chas.owens@gmail.com> writes:

Chas> Can this be modified to "Don't use prototypes until Perl 6"?

I consider Perl6 an entirely different language, so I'm speaking
on a Perl5 list, not a Perl6 list.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com