Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by stewart_lee (Novice)
on Nov 19, 2012 at 17:57 UTC ( #1004597=perlquestion: print w/ replies, xml ) Need Help??
stewart_lee has asked for the wisdom of the Perl Monks concerning the following question:

I have module Money/Finance.pm, within it, a sub getMovingAve is defined with protocol of ($$\@\@). I include this module in moveaverage2.pl, if I include it by 'use Money::Finance', I need call it as ($$@@), if I include it by 'require Money::Finance' I need call it as ($$\@\@), exactly matching with how its protocol is defined. Is it a bug or how to explain this difference. By the way, I run this test on HP box with Perl v5.8.8 and I have attached all both souce codes. Thanks in advance

$ cat moveaverage2.pl #!/usr/bin/perl -w #use strict; push(@Inc,'pwd'); #require Money::Finance; use Money::Finance; @values = ( 12,22,23,24,21,23,24,23,23,21,29,27,26,28 ); @mv = (0,10000); $size = scalar(@values); print "\n Values to work with = { @values } \n"; print " Number of values = $size \n"; #use strict; # ---------------------------------------------------------------- # Calculate the average of the above function # ---------------------------------------------------------------- my $cup = Money::Finance::new(); my $cup1 = new Money::Finance; $ave =Money::Finance::getLastAverage(5,$size,@values); print "\n Average of last 5 days = $ave \n"; Money::Finance::getMovingAve(5,$size,@values,@mv); print "\n Moving Average with 5 days window = \n { @mv } \n"; $ $ $ cd Money $ $ $ cat Finance.pm package Money::Finance; require Exporter; @ISA = (Exporter); @EXPORT = qw( new FutureValue PresentValue FVofAnnuity AnnuityOfFV getLastAverage getMovingAve SetInterest); # # Globals, if any # local $defaultInterest = 5.0; sub SetInterest($) { my $rate = shift(@_); $defaultInterest = $rate; printf "\n \$defaultInterest = $rate"; } # ------------------------------------------------------------------ # Notes: # 1. The interest rate $r is given in a value of [0-100]. # 2. The $n given in the terms is the rate at which the interest # is applied. # # ------------------------------------------------------------------ # ------------------------------------------------------------------ # Present value of an investment given # fv - a future value # r - rate per period # n - number of period # ------------------------------------------------------------------ sub FutureValue($$$) { my ($pv,$r,$n) = @_; my $fv = $pv * ((1 + ($r/100)) ** $n); return $fv; } # ------------------------------------------------------------------ # Present value of an investment given # fv - a future value # r - rate per period # n - number of period # ------------------------------------------------------------------ sub PresentValue($$$) { my $pv; my ($fv,$r,$n) = @_; $pv = $fv / ((1 + ($r/100)) ** $n); return $pv; } # ------------------------------------------------------------------ # Get the future value of an annuity given # mp - Monthly Payment of Annuity # r - rate per period # n - number of period # ------------------------------------------------------------------ sub FVofAnnuity($$$) { my $fv; my $oneR; my ($mp,$r,$n) = @_; $oneR = ( 1 + $r) ** $n; $fv = $mp * ( ($oneR - 1)/ $r); return $fv; } # ------------------------------------------------------------------ # Get the annuity from the following bits of information # r - rate per period # n - number of period # fv - Future Value # ------------------------------------------------------------------ sub AnnuityOfFV($$$) { my $mp; # mp - Monthly Payment of Annuity my $oneR; my ($fv,$r,$n) = @_; $oneR = ( 1 + $r) ** $n; $mp = $fv * ( $r/ ($oneR - 1)); return $mp; } # ------------------------------------------------------------------ # Get the average of the last "n" values in an array. # ------------------------------------------------------------------ # The last $count number of elements from the array in @values # The total number of elements in @values is in $number # sub getLastAverage($$@) { my ($count, $number, @values) = @_; my $i; my $a = 0; return 0 if ($count == 0); for ($i = 0; $i< $count; $i++) { $a += $values[$number - $i - 1]; } return $a / $count; } # ------------------------------------------------------------------ # Get a moving average of the values. # ------------------------------------------------------------------ # The window size is the first parameter, the number of items in the # passed array is next. (This can easily be calculated within the # function using the scalar() function, but the subroutine shown here # is also being used to illustrate how to pass pointers.) # The reference to the array of values is passed next, followed by a # reference to the place the return values are to be stored. # sub getMovingAve($$\@\@) { my ($count, $number, $values, $movingAve) = @_; my $i; my $a = 0; my $v = 0; return 0 if ($count == 0); return -1 if ($count > $number); return -2 if ($count < 2); $$movingAve[0] = 0; $$movingAve[$number - 1] = 0; for ($i=0; $i<$count;$i++) { $v = $$values[$i]; $a += $v / $count; $$movingAve[$i] = 0; } for ($i=$count; $i<$number;$i++) { $v = $$values[$i]; $a += $v / $count; $v = $$values[$i - $count - 1]; $a -= $v / $count; $$movingAve[$i] = $a; } return 0; } sub new { my $this = {}; # Create anonymous hash, and #self points to it. print "\n i am in new \n"; bless $this; # Connect the hash to the package Cocoa. return $this; # Return the reference to the hash. } 1; $

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

    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.

      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

      Thank you Athanasius, I got it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004597]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-12-28 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls