You might want to make the interface to that method a little more
consistent.
You pass in a list, but get back a reference.
If the list is expected to be small all/most of the time,
you might as well use lists. If you think it's going to
be big most of the time, go ahead and pass a reference in and out.

/dev

######### All Lists ##################3
  # assign keys (id's) to the librarian object
  $librarian->term_ids(1, 2, 3, 4);
 
  # print each key in $librarian->term_ids
  foreach ($librarian->term_ids) { print "id: $_\n" }
 
  sub term_ids {
 
      # get myself and then the ids
      my $self = shift;
      my @ids  = @_;

      # check for ids
      if (@ids) { $self->{term_ids} = [EMAIL PROTECTED] }

      # return
      return @{$self->{term_ids}};
 }

######### All References ##################3
  # assign keys (id's) to the librarian object
  $librarian->term_ids([1, 2, 3, 4]);
 
  # print each key in $librarian->term_ids
  foreach (@{$librarian->term_ids}) { print "id: $_\n" } 
 
  sub term_ids { 
 
      # get myself and then the ids
      my $self = shift;
      my $ids  = shift;

      # check for ids
      if (@ids) { $self->{term_ids} = [EMAIL PROTECTED] }

      # return
      return $self->{term_ids};
  }





> -----Original Message-----
> From: Eric Lease Morgan [mailto:[EMAIL PROTECTED]
> Sent: Thursday, September 25, 2003 9:29 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Inserting a list into an object's attribute
> 
> 
> On 9/25/03 7:15 AM, Paul Hoffman <[EMAIL PROTECTED]> wrote:
> 
> >> I suppose I could stuff a reference to the @ids array into
> >> $self->{term_ids} like this:
> >> 
> >>   if (@ids) { $self->{term_ids} = [EMAIL PROTECTED] }
> > 
> > Exactly!
> > 
> >> But then how do I dereference items in my object?
> > 
> > sub foo {
> >    my ($self) = @_;
> >    my $ids = $self->term_ids;
> >    # How to dereference a variable that holds an array ref:
> >    foreach (@$ids) { ... }
> > }
> > 
> > sub bar {
> >    my ($self) = @_;
> >    # How to dereference an expression that returns an array ref:
> >    my @ids = @{ $self->term_ids };
> >    foreach (@ids) { ... }
> > }
> 
> Thank you very much for the prompt reply. This is what I 
> needed to know, and
> this is how I implemented it:
> 
> 
>   # use the necessary module
>   use MyLibrary::Librarian;
>   
>   # create a librarian object
>   my $librarian = MyLibrary::Librarian->new();
>   
>   # assign keys (id's) to the librarian object
>   $librarian->term_ids(1, 2, 3, 4);
>   
>   # print each key in $librarian->term_ids
>   foreach (@{$librarian->term_ids}) { print "id: $_\n" }
>   
>   
>   sub term_ids {
>   
>       # get myself and then the ids
>       my $self = shift;
>       my @ids  = @_;
>           
>       # check for ids
>       if (@ids) { $self->{term_ids} = [EMAIL PROTECTED] }
>           
>       # return
>       return $self->{term_ids};
>   
>   }
> 
> 
> -- 
> Eric Morgan
> University Libraries of Notre Dame
> 
> (574) 631-8604
> 
> 
-- 
Devon Smith <[EMAIL PROTECTED]>
Software Engineer, Office of Research
OCLC Online Computer Library Center, Inc
http://www.oclc.org/research/
http://www.oclc.org/research/staff/smith/

Reply via email to