graq asked:

If I create a key,value pair in a hash(ref) which is stored in an array(ref) which is one of the methods on the object, will I need to rebuild the array and reset the property in order to effect the change?

Without actually running your code, everything appears to be correct. As for your question, it appears to be a bit vague. What do you mean 'create a key/value' pair? Are you referring to one of the pairs creating in the BEGIN block? Are you referring to updating a key/value pair for a particular supplier or or adding a new key/value pair on the fly? None of these should be particularly difficult.

In reading through your code, I'm not sure why you have the arrayref of hashrefs. It appears to me that this could be slow if you have many suppliers. The data structure that I am seeing looks like this (just the structure, I don't know the keys):

[ { 'supplier_id' => 17, 'item_foo' => 3, 'item_bar' => 7 }, { 'supplier_id' => 28, 'item_foo' => 5, 'item_qux' => 202 } ]

Unless the array index actually conveys useful information, I would probably turn this into a hashref of hashrefs with the primary hashs keys being the supplier_id:

{ '28' => { 'item_foo' => 5, 'item_qux' => 202 }, '17' => { 'item_foo' => 3, 'item_bar' => 7 } };

If this works for you, your &set_stock sub becomes simpler and faster:

sub set_stock { my( $self, $supplier, $line, $stockLevel ) = @_; my $supplierList = (); my $supplierNumber = $supplier->supplier_id; # $supplierList will be a reference to a hash of references to has +hes. unless( $supplierList = $self->get('supplier_list') ) { die( "Cannot set stock level when supplier list is corrupt: $s +upplierList" ); } my $reference = $line->reference; $supp->{ $supplierNumber }{ $reference } = $stockLevel; }

Now that I've gotten thoroughly off-topic, what was your question again? :) If you can provide more detail of what problem you're trying to solve...

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Storing Info with Accessors by Ovid
in thread Storing Info with Accessors by graq

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.