fmogavero has asked for the wisdom of the Perl Monks concerning the following question:

Brethern and cistern,
I have an array @temparray that I would like to put into an object's property $self->clientorgunit.

The property is defined thus: $self->{clientorgunit} = [];.

$self->clientorgunit(@temparray); puts only the first element of the array into $self->clientorgunit.

$self->{clientorgunit} = @temparray; puts only the scalar value of @temparray into $self->clientorgunit.

What am I missing here?

Thanks,
fmogavero

Replies are listed 'Best First'.
Re: Putting contents of an array into Object property
by Masem (Monsignor) on Sep 28, 2001 at 17:51 UTC
    Your $self->{clientorgunit} is a refernce to an array, so you need to deref it to make it an array before you put things into it. You could do it either as:
    @{$self->{clientorgunit}} = @items_to_put;
    Or, better, if you are continuing to add items to it:
    push @{$self->{clientorgunit}}, @items_to_put;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Putting contents of an array into Object property
by davorg (Chancellor) on Sep 28, 2001 at 17:53 UTC

    It looks like your object is defined as a blessed hash. Each value in a hash must be a scalar. You can't store an array in a scalar. You can, however, store a reference to an array in a scalar.

    $self->clientorgunit(\@temparray);

    or

    $self->{clientorgunit} = \@temparray;

    should do the trick :)

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: Putting contents of an array into Object property
by arturo (Vicar) on Sep 28, 2001 at 18:34 UTC

    The other solutions mention how to do things with an array reference. That's probably what you want, but just because there are other ways (so I've heard), if you want your method to accept arrrays for whatever reason, you could do something along the following lines in your method:

    sub clientcrgunit { my ($self, @stuff) = @_; $self->{clientcrgunit} = \@stuff; }

    The first thing passed to the method is the reference to the object, the rest will be slurped into @stuff; if you want to accept either arrays *or* array references, you could check to see whether @stuff has one member, and if that member is a reference to an array:

    sub clientcrgunit { my ($self, @stuff) = @_; if ( @stuff == 1 && ref($stuff[0] eq 'ARRAY') ) { $self->{clientcrgunit} = $stuff[0]; } else { $self->{clientcrgunit} = \@stuff; } }
    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'

      I ++ this method. But I wanted to note that there is an extra advantage to this method over using \@stuff as advocated by several other people. Doing, for example:

      $self->{clientcrgunit}= \@stuff; @stuff= getNextBatchOfStuff();
      will cause $self->{clientcrgunit} to be modified to hold the list returned by getNextBatchOfStuff(). So, if you don't go with arturo's solution, you should probably use: $self->{clientcrgunit}= [@stuff]; instead. You can avoid this extra copying if you know that @stuff is a lexical variable that won't be modified again until it is destroyed.

              - tye (but my friends call me "Tye")