This may not help you as it requires modification of your subroutines and assumes they will always be used together. It makes use of the fact that calling a subroutine from inside another in the form &sub_name without any parentheses or arguments passes the arguments (@_) of the calling routine to the called one. Obviously, modification of arguments before calling subsequent routines will break this model and careful consideration of order is required, e.g. we can't calculate the average before we've got the sum.

$ perl -Mstrict -Mwarnings -E ' my $rsSum = \ do { my $dummy }; my $rsAvg = \ do { my $dummy }; sub sum (@) { ${ $rsSum } += $_ for @_; &avg; } sub avg (@) { ${ $rsAvg } = ${ $rsSum } / scalar @_; } sum( 3, 4, 5, 6, 7 ); say qq{Sum - ${ $rsSum }\nAvg - ${ $rsAvg }};' Sum - 25 Avg - 5 $

I hope this is of interest.

Update: Regarding order, calling &sum from sub avg before calculating the average also works.

$ perl -Mstrict -Mwarnings -E ' my $rsSum = \ do { my $dummy }; my $rsAvg = \ do { my $dummy }; sub avg (@) { ∑ ${ $rsAvg } = ${ $rsSum } / scalar @_; } sub sum (@) { ${ $rsSum } += $_ for @_; } avg( 3, 4, 5, 6, 7 ); say qq{Sum - ${ $rsSum }\nAvg - ${ $rsAvg }};' Sum - 25 Avg - 5 $

Cheers,

JohnGG


In reply to Re: multiple consumer of an array by johngg
in thread multiple consumer of an array by bagyi

Title:
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, details, 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, summary, 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.