http://www.perlmonks.org?node_id=1004658


in reply to Re^2: Any difference between use and require regarding honoring prototype defined for sub? (prototype)
in thread Any difference between use and require regarding honoring prototype defined for sub?

Hello stewart_lee, and welcome to the Monastery!

I use 'use', then I should call the sub with ($$@@) instead of ($$\@\@)

I think you may be confused about how prototypes work in Perl. Specifically, ($$\@\@) means the subroutine expects:

The backslash in the prototype — \@ — means the array will be converted into an array reference inside the sub. So, in your example,

Money::Finance::getMovingAve(5,$size,@values,@mv); ... sub getMovingAve($$\@\@) { my ($count, $number, $values, $movingAve) = @_; ...

the variables $values and $movingAve are assigned references to the arrays @values and @mv, respectively, by virtue of the subroutine’s prototype.

So, with use, the code is behaving as expected. But with require, the prototype is ignored because it is seen too late, so to make the sub work correctly you have to pass references explicitly.

Perl prototypes are confusing, and should probably be avoided unless you have a good reason to use them. Make sure to read Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen.

By the way, you would probably have received help sooner had you posted a trimmed-down (but still working) version of the code showing only the relevant parts. See How do I post a question effectively?

And do not comment out use strict; — it’s there to make your life easier!

Hope that helps,

Athanasius <°(((><contra mundum