Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
composite pattern, best practice, pushing into an array in a hash, reusing the get array methodby gargle (Chaplain) |
on Feb 13, 2006 at 20:41 UTC ( [id://529928]=perlquestion: print w/replies, xml ) | Need Help?? |
gargle has asked for the wisdom of the Perl Monks concerning the following question: Dear Monks, I'm a bit puzzled... :( I have implemented a composite pattern and wanted to reuse a sub called getCreditAccounts() I have already written down. getBalansCreditAmount() calls all accounts in the CREDITACCOUNTS array and reapplies itself to get the credit amounts stored there. getCreditAccounts(), used by getBalansCreditAmount() returns an array with accounts. (the sub is listed below) I also have a addCreditAcount() to add accounts to the CREDITACCOUNTS array. In this sub I first fetch the parameter, a new account. Then I want to get the contents of CREDITACCOUNTS and push the new account in it. I do this byy taking the reference from &{ $closure }("CREDITACCOUNTS") in an $accountref and pushing the new account in the de-reference.(the sub is listed below) However, I feel like I didn't reuse getCreditAccounts() because of the funny looking my $accountsref = &{ $closure }("CREDITACCOUNTS") my constructor
addCreditAccount()
getBalansCreditAmount()
getCreditAccounts()
A a lot of retyping seems to just have happened. Is there a way to reuse getCreditAccounts() in the addCreditAccount sub to make the code a bit clearer to understand? ps: I need to check for circular references in this code. This I'll implement this later!
-- if ( 1 ) { $postman->ring() for (1..2); }
Back to
Seekers of Perl Wisdom
|
|