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
sub new { my $class = shift; my $name = shift; my $self = { NAME => $name, CREDITAMOUNT => Math::BigInt->new(0), DEBETAMOUNT => Math::BigInt->new(0), CREDITACCOUNTS => [], DEBETACCOUNTS => [], }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless( $closure, $class ); return $closure; }
addCreditAccount()
# public to add accounts to the creditaccounts sub addCreditAccount { my $closure = shift; my $account = shift; my $accountsref = &{$closure}( "CREDITACCOUNTS" ); push @{ $accountsref }, $account; }
getBalansCreditAmount()
# public getBalansCreditAmount to get the amount of all accounts sub getBalansCreditAmount { my $closure = shift; my $totalCreditAmount = $closure->getCreditAmount(); for my $balans ( $closure->getCreditAccounts() ) { $totalCreditAmount += $balans->getBalansCreditAmount(); } return $totalCreditAmount; }
getCreditAccounts()
# private getCreditAccounts sub getCreditAccounts { caller(0) eq __PACKAGE__ || confess "getCreditAccounts is private" +; my $closure = shift; my @accounts = @{ &{$closure}( "CREDITACCOUNTS" ) }; return @accounts; }

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); }

In reply to composite pattern, best practice, pushing into an array in a hash, reusing the get array method by gargle

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.