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

I'm getting (to me) counter intuitive results.

The code (problematic lines 14 & 15 are put between #====s):

@gum = qw(foo noo goo); @do = qw(test); package test; sub new {my $pkg = shift; bless { name=> "test", array=>\@gum } } sub express{ my ($self, $body) = @_; #======================== print "$body: $self->{name} ", ref $self->{array}," $self->{array}\n" +; print "@$self->{array}\n"; #========================= } package main; foreach $type (@do){ $moo = new $type(); $moo->express("here it is"); }
yields the following output:

C:\apache\cgi-bin\test>perl phah.pl here it is: test ARRAY ARRAY(0x1824778) Not an ARRAY reference at phah.pl line 15.
But the output of line 14 implies that it is an array reference. What's going on?

BTW, I'm using Hall and Schwartz's "Effective Perl Programming" as reference and guidelines. Can/Should I blame my operating system? (Windows XP; always a fun target for blame casting...)

Replies are listed 'Best First'.
Re: references to arrays. I don't really get it I guess...
by jweed (Chaplain) on Dec 02, 2003 at 00:26 UTC
    You need braces:
    print "@{$self->{array}}\n";
    UPDATE
    Just thought I should point out that the @gum in line 1 is not the same @gum from line 9. Which is why you get a blank line after your first one in the correct output. Just FYI.
    UPDATE 2
    Of courses you can blame XP! But, perhaps you shouldn't in this case.


    Who is Kayser Söze?
      Okay, but then...

      Why does:

      print "foo\n"; print "@{$test->{gum}}\n"; if (grep(/^$tag$/), @{$test->{gum}}){ ...
      give me:
      foo Not an ARRAY reference at line 16 foo noo goo
      in particular why does the error for line 16 occur BEFORE the output for line 15? And what is the error of line 16? Something to do with grep?

      I'm sorry I'm being dense. (Stupid windows XP. Ah, that was fun.)

        Aargh. All fixed now. Was another braces problem on another line (the elsif condition) that was only evaluated when the if condition was false. I'm a little confused why the error sometimes appeared before the line above and why the line number was the line number of the if clause and not the line number real culprit, the elsif clause.

        Oh, well. fixed now. Thanks everyone.

Re: references to arrays. I don't really get it I guess...
by mpeppler (Vicar) on Dec 02, 2003 at 00:28 UTC
    The "Not an ARRAY ref" error is due to precendence rules. Consider:
    print "@{$self->{array}}"; vs print "@$self->{array}";
    That said - there are a number of other problems with your code that will show up once you "use strict;".

    Michael

      Thanks. The braces makes a lot of sense. The thing was in my real code I still got errors when I used braces. But this was actually from another iteration where $self was a different thing altogether but the error looked the same so I assumed it was. Uh, what sort of "number of other problems". Might as well find out about them now rather than later.
        Uh, what sort of "number of other problems". Might as well find out about them now rather than later.
        Why don't you "use strict;" and find out?
        The first problem is your @gum array. You initialize it in package "main", but you reference it in package "test". Which means that you are accessing two different @gum arrays: @main::gum and @test::gum. Using "strict" would point that out (among other things).

        Michael

Re: references to arrays. I don't really get it I guess...
by duff (Parson) on Dec 02, 2003 at 00:52 UTC

    As others have said, you need to put curly braces around $self->{array} so that the @ dereferences the correct thing. @$self->{array} is equivalent to @{$self}->{array} as Perl will dereference the simple scalar or BLOCK that follows the @. In my code I almost always put the braces even for the simple cases just so that there is no ambiguity. So, again, @{$self->{array}} is what you want.

Re: references to arrays. I don't really get it I guess...
by TomDLux (Vicar) on Dec 02, 2003 at 04:32 UTC

    Begin by blaming the student. After you have examined every line of code and read the relevant perldocs, you can consider finding other things to blame.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      The comment about blaming windows was a joke.

      I did examine every line of code and read the relevent perldocs. In the end the error was that I wasn't using curly brackets but not on the line claiming the error. Until I discovered any false clause caused the error, everything appeared that that an array ref was suddenly not being an array ref. I knew it had to be something I did but when the line claiming to give the error was fine and the perldocs agreed with it, I was at a bit of a loss.