You could create the array in processA() / processB() and pass a reference to it to commonStuff() as a parameter:

processAorB ....{ my @commonArray; commonStuff( \@commonArray, \%other, \$variables ); ... } sub commonStuff { my ( $commonArrayRef, $hash, $var ) = @_; if ( $commonArrayRef->[0] > 37 ) { ... } }
As you can see, accessing the $commonArrayRef contents is just like handling the array itself, except for sticking an arrow in the middle. There's only one array, it's only a reference which is passed around, rather than copying the array, so function calls are faster, and any changes to the array within commonStuff() is visible where it is called.

If you don't need the array outside commonStuff(), you can use a closure. The one trick would be detecting when you go from processA() to processB() ... I'm assuming the array should be re-initialized at that point. Hopefully, there is some way of detecting within commonStuff() when the time has come to re-initialize. Alternately, you could provide a second closure method for re-initializing the closure, and invoke that at appropriate times from processA() / processB().

sub processA { ... commonInit() commonStuff( $various, $vars ); } sub processB { ... commonInit() commonStuff( $various, $other, $vars ); } { my @commonArray; sub commonInit { @commonArray = (); } sub commonStuff { # do stuff to @commonArray; } }

This way, you can preserve your data without polluting the entire environment. You might also split the common stuff into several routines.

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: Design Question by TomDLux
in thread Design Question by Anonymous Monk

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.