Array as instance variable

Discussion in 'Perl Misc' started by robert.waters, Dec 10, 2006.

  1. 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)
     
    robert.waters, Dec 10, 2006
    #1
    1. Advertising

  2. Also, how would I go about populating that array inside the constructor?
     
    robert.waters, Dec 10, 2006
    #2
    1. Advertising

  3. robert.waters

    Uri Guttman Guest

    >>>>> "rw" == robert waters <> 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.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 11, 2006
    #3
  4. robert.waters

    -berlin.de Guest

    Uri Guttman <> wrote in comp.lang.perl.misc:
    > >>>>> "rw" == robert waters <> 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
     
    -berlin.de, Dec 11, 2006
    #4
  5. robert.waters

    Uri Guttman Guest

    >>>>> "a" == anno4000 <-berlin.de> writes:

    a> Uri Guttman <> wrote in comp.lang.perl.misc:
    >> >>>>> "rw" == robert waters <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 11, 2006
    #5
  6. robert.waters

    -berlin.de Guest

    Uri Guttman <> wrote in comp.lang.perl.misc:
    > >>>>> "a" == anno4000 <-berlin.de> writes:

    > a> Uri Guttman <> wrote in comp.lang.perl.misc:
    > >> >>>>> "rw" == robert waters <> 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
     
    -berlin.de, Dec 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Carey

    Instance Variable vs Local Variable

    Paul Carey, Dec 3, 2003, in forum: Java
    Replies:
    3
    Views:
    810
    Chris Uppal
    Dec 3, 2003
  2. Jerry
    Replies:
    5
    Views:
    32,760
    Roedy Green
    Aug 6, 2005
  3. Gerry Sutton
    Replies:
    1
    Views:
    540
    Peter Otten
    Apr 16, 2005
  4. David Garamond
    Replies:
    5
    Views:
    249
    Ara.T.Howard
    Jun 8, 2004
  5. Leon Bogaert
    Replies:
    19
    Views:
    328
    Robert Klemme
    Mar 23, 2008
Loading...

Share This Page