Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Array as instance variable

Reply
Thread Tools

Array as instance variable

 
 
robert.waters
Guest
Posts: n/a
 
      12-10-2006
Hello,

I need to keep track of a list of items in my package/object;
currently, I am going about it like this:

sub new {
my $self = { _listofstuff => undef };
bless $self;
return $self;
}
sub dostuff {
my $self =shift;
for (my $i=0; $i<10 ;$i++) {
push @{$self->{_listofstuff}), "listitem";
}
}

Is there a better way? Using '$self->underscored-hash-item' is
tedious, but I am willing to deal with it; however, having to treat
this hash element as an array in order to use it as such (rather than
declaring it as an array to begin with) seems hackish. I would very
much love to just see '@listofstuff'.

Can't I just declare an array in the top-level scope and pass it around
as a reference from function to function? Or would this create a class
variable, shared by each instance?

Thank you very much,
Robert
(a perl OOP beginner)

 
Reply With Quote
 
 
 
 
robert.waters
Guest
Posts: n/a
 
      12-10-2006
Also, how would I go about populating that array inside the constructor?

 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      12-11-2006
>>>>> "rw" == robert waters <(E-Mail Removed)> 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).

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      12-11-2006
Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>>>> "rw" == robert waters <(E-Mail Removed)> 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).


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

Anno
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-11-2006
>>>>> "a" == anno4000 <(E-Mail Removed)-berlin.de> writes:

a> Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> >>>>> "rw" == robert waters <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      12-12-2006
Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>>>> "a" == anno4000 <(E-Mail Removed)-berlin.de> writes:

> a> Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >> >>>>> "rw" == robert waters <(E-Mail Removed)> writes:

>
> >> 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'!).


The same technique applies also when a field is a plain scalar instead
of an array (or other) reference. Instead of accessing $self->{ scalar}
repeatedly, do

my $scalar = $self->{ scalar};

and use that.

One difference is that with a reference after

my $array = $self->{ array};

you have full access to the underlying array and can change its
content through $array. You can't change the content of
$self->{ scalar} through $scalar because $scalar is a copy.

If you need that (which isn't always the case) you can use a one-shot
loop to get a lexical alias for $self->{ array}:

for my $scalar ( $self->{ scalar} ) {
# use $scalar here, including assignment
$scalar = 123; # changes $self->{ scalar}
}

That gets clumsy if you have more than one or two aliases to set up
because you need to nest a block for each one. There are a couple
of modules on CPAN that give you lexical aliases through other means
which could help there (search for "lexical alias") . However, in
that situation I would probably conclude I was trying to do too much
in a single method and change the design.

Anno


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index of an array containing an instance, from this instance Paul A. Ruby 2 12-30-2009 10:56 PM
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
Instance Variable vs Local Variable Paul Carey Java 3 12-03-2003 05:05 PM



Advertisments