Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

by Anonymous Monk
on Nov 19, 2012 at 18:13 UTC ( #1004600=note: print w/ replies, xml ) Need Help??


in reply to Any difference between use and require regarding honoring prototype defined for sub?

Yes, there is a difference, use use and not require

should be documented in perlsub

if you use require, you'll need to use a forward declaration

$ perl -le "sub f($$@@){warn qq{@_}} f(@ARGV,@ARGV,@ARGV); " 1 2 3 4 5 5 5 1 2 3 4 5 at -e line 1. ## simulate "require" $ 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. ## forward/early/prototype declaration $ perl -le " sub f($$@@); eval q{sub f($$@@){warn qq{@_}}}; f(@ARGV,@A +RGV,@ARGV); " 1 2 3 4 5 5 5 1 2 3 4 5 at (eval 1) line 1.


Comment on Re: Any difference between use and require regarding honoring prototype defined for sub? (prototype)
Select or Download Code
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

    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.

      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

Re^2: Any difference between use and require regarding honoring prototype defined for sub? (prototype)
by stewart_lee (Novice) on Nov 21, 2012 at 17:27 UTC

    Thank you Athanasius, I got it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-11-27 02:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (178 votes), past polls