Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by stewart_lee (Novice)
on Nov 19, 2012 at 19:56 UTC ( #1004608=note: print w/ replies, xml ) Need Help??


in reply to Re: 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?

Thanks, what I read the difference between use and require is that module included by use is loaded in compilation while it is loaded in execution by require, but they won't cause any difference on hornoring the protocol defined for the sub. But in my example, if I use 'use', then I should call the sub with ($$@@) instead of ($$\@\@) as defined for sub's protocol. Also, when trying your 3 commands, only the second one seems good.

$ perl -le "sub f($$@@){warn qq{@_}}; f(@ARGV,@ARGV,@ARGV); " 1 2 3 4 +5 Malformed prototype for main::f: 29673@@ at -e line 1. $ perl -le " eval q{sub f($$@@){warn qq{@_}}}; f(@ARGV,@ARGV,@ARGV); " + 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 at (eval 1) line 1. $ perl -le " sub f($$@@); eval q{sub f($$@@){warn qq{@_}}}; f(@ARGV,@A +RGV,@ARGV); " 1 2 3 4 5 Malformed prototype for main::f: 29673@@ at -e line 1.


Comment on Re^2: Any difference between use and require regarding honoring prototype defined for sub? (prototype)
Download Code
Re^3: Any difference between use and require regarding honoring prototype defined for sub?
by Athanasius (Monsignor) on Nov 20, 2012 at 04:50 UTC

    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:

    • a scalar
    • another scalar
    • an array (not an array reference!)
    • another array

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-22 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls