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

How did you come to the conclusion that it is a AoA? In the code you're showing, it is a reference to an array. But, since it is a package variable, it may be that you change the contents of @log_lines somewhere else in your package, and thus the reference to it changes as well...

Paul

Replies are listed 'Best First'.
Re^2: Blessing object changes array attribute in array of arrays?
by mclow (Initiate) on Jul 06, 2005 at 14:56 UTC
    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.
      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.

      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. :-)