Much as I thought my understanding of references was going well, again misunderstanding strikes and I seek your wisdom.

The following code is a bare-bones test and demonstration of the issue I am having. In the module's new method I read an array from a database which is simulated here by @columns.

package crmtest; use strict; use warnings; sub new { my ($class, $env) = @_; my @columns = ('one', 'two', 'three', 'four'); my $self = bless { 'env' => $env, 'column' => \@columns, # <-- Reference t +o an array given here }, $class; return $self; } sub add { my ($self, $fields) = @_; foreach my $field(@$self->{'column'}) { # <-- Not an AR +RAY reference here print $field . " -- " . $self->{'column'} . "\n"; # do stuff... } } 1;

Here is what I think is happening...
I put a reference to the @columns array into the blessed anonymous hash to which $self holds the reference. The constructor returns the reference to the anonymous hash.
Later on, in the add method I want to iterate over the array so I dereference the key in the anonymous hash using @$self->{'column'}

Except I get the error - Not an ARRAY reference at crmtest.pm line 18
So I added the print line and removed the dereferencing so I tried iterating over $self->{'column'} without the '@'. This tells me that both $field and $self->{'column'} are the same as I would expect and that they are both ARRAY(0xe3c1f0)

This is the code I am using to test the bare-bones of the module:

use strict; use warnings; use lib('.'); use crmtest; my $crm = crmtest->new('someenv'); my $cols = { 'col1' => 'something', 'col2' => 'anotherthing', 'four' => 'moretest', }; $crm->add($cols);
Am I not creating the reference to @columns properly or am I trying to dereference wrongly or I am completely missing how references work?

EDIT:
To test what I am doing I have tried an even simpler version:

my @columns = ('one', 'two', 'three', 'four'); my $ref = \@columns; print @$ref;
And that does exactly what I expected - prints out the array. So it seems I am not totally on the wrong track!


In reply to Not an ARRAY reference error by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.