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

It must be in how you're inspecting the object, because $self->{log_lines} should be a reference to a single array. But if that's the actual code you're posting, then the code won't do what you want anyway, because you're using references to the global variable @log_lines, and so all your objects will reference the same array instead of getting a new array.

You should be using the strict pragma and change your code to read

... local *LOG_FILE; open LOG_FILE, '<', $fullpath or die "Couldn't open logfile '$fullpath': $!"; my @log_lines = <LOG_FILE>; ...

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:07 UTC
    Thanks for your reply Corion,

    while I'm still trying to debug the 'ARRAY'->'ARRAY of ARRAYS' problem, I've started using the 'strict' pragma.

    Is a 'my' variable in the constructor always readable by the object INSTANCE?
    In our case, for example, what if in the constructor I set a reference to my @log_lines?

    mc

      You seem to be unaware of scoping. The best article I know about scoping is Coping With Scoping, which explains you all the stuff to know about global variables and lexical variables.

      For the problem at hand, yes, declaring @log_lines via my is exactly what you need, because then, each reference taken to @log_lines will be unique and point to its own incarnation of @log_lines, which seems to be what you want as $self->{log_lines} seems to be intended to be an instance variable and not a class/package variable.

      You also seem to have a misconception about how objects work in Perl, stemming from how you (don't) interpret scope. Variables declared via my are lexical variables that are only visible (under their name) within the closest set of enclosing curly braces ({}). An object usually is a (blessed) reference to a hash and you have to stuff all instance variables into that hash. The declaration in the constructor is not really related to that.