Re: Array as instance variable



"a" == anno4000 <anno4000@xxxxxxxxxxxxxxxxxxxxxx> writes:

a> Uri Guttman <uri@xxxxxxxxxxxxxxx> wrote in comp.lang.perl.misc:
>> >>>>> "rw" == robert waters <robert.waters@xxxxxxxxx> writes:
>>
rw> Also, how would I go about populating that array inside the
rw> constructor?
>>
>> this is in response to BOTH of your postings. please even quote yourself
>> if you have to do this.
>>
>> you can use ANY reference for an object, not just hashes. hashes just
>> are the most common and usually direct way to do perl objects. get the
>> book Object Oriented Perl by damian conway and learn how to make objects
>> out of any ref including arrays. that will also show you how to
>> initialize them (hint: just the same as with any array you need to
>> init).

a> That is an excellent solution if the array is the only data to be
a> kept in the object. If the array is just one field among others,
a> there is no other way than than to use an arrayref much like the OP
a> did.

and even if he uses a hash ref for the object and it has the array ref,
there is a very simple way to clean up the code of all those
$self->{array} expressions. just assign the array ref to a lexical in
the methods and use that. it is called factoring out common (or
constant) expressions and it is a very old coding and compiling
technique. you can think of it as useful case of a temp variable (just
don't ever name it 'temp'!).

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.



Relevant Pages

  • Re: Ruby expressions vs. values (like Perl?)
    ... Perl, one needs to be well aware of the difference between expressions ... So, back to Ruby, I believe that the rhs of the following statement is ... an "Array expression": ...
    (comp.lang.ruby)
  • Re: Fwd: Re: dbm again
    ... The hash has only one key, ... Can't use string ) as an ARRAY ref while strict refs in ... It seems like I am somehow dealing with a reference to an array!? ... as a string and tried to be derefenced. ...
    (perl.beginners)
  • Ruby expressions vs. values (like Perl?)
    ... Perl, one needs to be well aware of the difference between expressions ... So, back to Ruby, I believe that the rhs of the following statement is ... parallel assignment -- also happens to be generating an Array value: ...
    (comp.lang.ruby)
  • Re: Modifiying array elements with references
    ... > be able to modify some array elements through reference, ... > don't want a new array. ... Are you aware that you could also modify the ... but the use of an array ref is ...
    (comp.lang.perl.misc)
  • Re: removing element of array
    ... Charles K. Clarkson wrote: ... I tried to write a small test script to figure this out, ... I used array references in my ... > the original array ref. ...
    (perl.beginners)