"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?"

I think the code gives a more specific example of what I am asking.


Accessor building:

# -------------------------------------------------------------------- +---------- # OBJECT CREATION # -------------------------------------------------------------------- +---------- sub _create_accessors { my %OrderObjectFields = @_; foreach my $field (keys %OrderObjectFields) { my $default = $OrderObjectFields{ $field }; my $code = ' sub '.$field.' { my $value = (scalar @_>1? $_[0]->{"'.$field.'"} = $_[1 +] : $_[0]->{"'.$field.'"} + ); $value ="'.$default.'" unless defined $value; $value; }'; eval $code; die $@ if( $@ ); } } # -------------------------------------------------------------------- +---------- BEGIN { _create_accessors( 'order_no' => 0 , 'items_in_order' => '', 'supplier_list' => '', ); } # -------------------------------------------------------------------- +---------- # STANDARD OBJECT METHODS # -------------------------------------------------------------------- +---------- sub get { my( $self, $attribute ) = @_; return $self->{$attribute}; } # -------------------------------------------------------------------- +---------- sub set { my( $self, $attribute, $value ) = @_; $self->{$attribute} = $value; }
Which I am happy with (but no doubt someone will prove TIMTOWODI! ;P)

This is the code snippet which my question is based on:

sub set_stock { my( $self, $supplier, $line, $stockLevel ) = @_; my $supplierList = (); my $supplierNumber = $supplier->supplier_id; # $supplierList will be a reference to an array of references to h +ashes. unless( $supplierList = $self->get('supplier_list') ) { die( "Cannot set stock level when supplier list is corrupt: $s +upplierList" ); } foreach my $supp (@$supplierList) { next unless( $supp->{supplier_id} == $supplierNumber ); my $reference = $line->reference; # Here is the update. $supp->{$reference} = $stockLevel; } }
Having reached this point in the code, I became unsure whether I would have to splice @$supplerList or in some other way rebuild it, and then reassign it with $self->set

I just feel that it is starting to look messy and smacks of just not being right. I'd love some guidance on approaching this problem.

The more I preview this, the more I feel I am missing something obvious .. *goes back to reading the panther book some more*

--
Graq


In reply to 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.