Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re^3: Any difference between use and require regarding honoring prototype defined for sub? by Athanasius
in thread Any difference between use and require regarding honoring prototype defined for sub? by stewart_lee

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (7)
    As of 2018-06-19 09:44 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (112 votes). Check out past polls.