in reply to unshifting by reference

unshift(@($this->{Memory}),$this->{Senses});

To dereference the array ref, you need curlies, not parentheses, i.e. @{$this->{Memory}}

my $this = { Memory => ["foo"], Senses => "bar", }; unshift @{$this->{Memory}}, $this->{Senses}; use Data::Dumper; print Dumper $this; __END__ $VAR1 = { 'Senses' => 'bar', 'Memory' => [ 'bar', 'foo' ] };

Replies are listed 'Best First'.
Re^2: unshifting by reference
by palkia (Monk) on Dec 16, 2011 at 00:43 UTC
    Thx it works (obvious it was ^^).
    But I don't et it, I thought that the "->" dereferences, doesn't it ?
    when I did print $this->{N}[0]{Value}; it worked ok, so whats the difference ?
    Why didn't I needed to dereference there ?
    and whats "Dumper" is this really required ? (I prefer to avoid module dependencies)
    Thx again

      The thing is that unshift needs an array (syntactically), not an array reference. And the way to get an (entire) array from a reference is to say @$aref, or @{ $something->{that}{returns}[1]{aref} } for more complex expressions.  To address a single element of the array, you'd say $aref->[$index].

      (The Data::Dumper module is to easily print out complex data structures. I just used it for demo purposes — you of course don't need it to unshift the value.)

      -> is dereferencing a hash that has nothing to do with your question.

      Your question is about dereferencing the array referenced by $this->{N}.

      -> is dereferencing a hash that has nothing to do with your question.

      Your question is about dereferencing the array referenced by $this->{N}.