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

I'm still relatively new to perl, so I'm sorry if this makes no sense. I'm writing a program that controls five circuits in our lab, and automates the running of tests on those circuits. I'm using Gtk2 and a module to make the overly complicated tree view into a simple list. According to the author of the module, simplelists can be treated as arrays. At one point, I am trying to shift something off of one simple list and push it onto the other. Trouble is, when I do this, I get what looks to me like a pointer. Something like this "ARRAY(0x98630b8)". This seemed to work fine last week, so I don't know why it changed its mind. I put a print right after my shift statement to see if I could isolate the problem, and that is what it prints out. Can someone tell me why I am getting this value and not the element in the array?

$environment = shift @{$Envs->{data}}; print $environment;

Replies are listed 'Best First'.
Re: Shift returning pointer
by toolic (Bishop) on Mar 16, 2009 at 16:58 UTC
    You can use Data::Dumper to look at arbitrarily complex Perl data structures (tip #4 from Basic debugging checklist):
    use Data::Dumper; # choose one of these, depending on how you initially declared Envs #print Dumper($Envs); #print Dumper(\@Envs); #print Dumper(\%Envs); print Dumper($environment);
Re: Shift returning pointer
by bellaire (Hermit) on Mar 16, 2009 at 16:23 UTC
    Your shift looks fine, my guess is that the problem is at the point where $Envs->{data} is assigned. Can we see that code, please?

      Thanks guys, that makes sense to a degree. I'm not sure why it changes to array refs though. Here is my code where I am assigning what is in the list.

      open(ENVIRONMENTS, "environments.txt"); while(<ENVIRONMENTS>){ my $line = $_; chomp($line); push @{$Envs->{data}}, [$line];
        The square brackets around $line put it into an array ref before pushing it onto @{$Envs->{data}}. Drop them, and it'll work the way you want:
        push @{$Envs->{data}}, $line;
Re: Shift returning pointer
by ELISHEVA (Prior) on Mar 16, 2009 at 16:25 UTC
    What you are seeing print out is a reference to an array. See perlref for more information about references.

    Best, beth

Re: Shift returning pointer
by shmem (Chancellor) on Mar 16, 2009 at 16:29 UTC

    What bellaire said; and to see what's in there, try

    print ref $environment eq 'ARRAY' ? join(', ', map { "'$_'" } @$environment) : $environment;
Re: Shift returning pointer
by Bloodnok (Vicar) on Mar 16, 2009 at 16:25 UTC
    Could it be that the data has changed from an array of scalars, to an array of array refs (AoA)?

    A user level that continues to overstate my experience :-))