You could create the array in processA() / processB() and pass a reference to it to commonStuff() as a parameter:
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.processAorB ....{ my @commonArray; commonStuff( \@commonArray, \%other, \$variables ); ... } sub commonStuff { my ( $commonArrayRef, $hash, $var ) = @_; if ( $commonArrayRef->[0] > 37 ) { ... } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |