in reply to Re: Blessing object changes array attribute in array of arrays?
in thread Blessing object changes array attribute into array of arrays?

I've extracted the fault code and I still get the same problem (AoA):

#!/usr/bin/perl -w package McTest; use strict; sub new { my ($class, $path) = @_; print $path . "\n"; open LOG_FILE, '<', $path; my @lines = <LOG_FILE>; close LOG_FILE; my $self = { loglines => \@lines }; bless($self, $class); return $self; } my $mctest = McTest->new("access.log"); # referenced version foreach my $l($mctest->{loglines}) { print $l . "\n"; } # unreferenced version my @unref_array = $mctest->{loglines}; foreach my $l(@unref_array) { print $l . "\n"; }

Any hints?
Mc.

Replies are listed 'Best First'.
Re^3: Blessing object changes array attribute in array of arrays?
by dave_the_m (Monsignor) on Jul 06, 2005 at 15:01 UTC
    foreach my $l ($mctest->{loglines}) { print $l . "\n"; }
    That should be
    foreach my $l(@{$mctest->{loglines}}) { print $l . "\n"; }
    $mctest->{loglines} is a reference to an array; @{$mctest->{loglines}} is that array.

    Dave.

Re^3: Blessing object changes array attribute in array of arrays?
by Tanktalus (Canon) on Jul 06, 2005 at 15:58 UTC

    As dave_the_m points out, you're not dereferencing quite properly. Whenever I get output that doesn't match what I think it should, I always (always always) resort to Data::Dumper. Add something like this to your code:

    use Data::Dumper; print Dumper($mctest);
    You'll see that it's not an array of arrays very quickly this way. And that should lead you to suspect you're using it incorrectly. You may still have the question of how to dereference incorrectly, but at least it'll be the right question. :-)

    Data::Dumper has given me many a forehead-slapping "d'oh!" moment. Why? Because it has already had all the derefencing bugs worked out. It just works. My code generally isn't quite so reliable. :-)