Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Data inheritance

by perlbaski (Sexton)
on Sep 23, 2011 at 17:18 UTC ( [id://927588]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, I am new to writing oo perl. I have two modules(CommonObject.PM and MyObject.PM) MyObject.pm inherits from CommonObject.
package CommonObject; my @common_array; package MyObject; @ISA = qw( CommonObject ); sub new{ my $class=shift; my $self = {}; bless $self, $class; $self->@common_array=qw(1 2);#This line throws syntax error return $self; }

How do i set array variables in parent class?

I know it has to do with $self being a reference to a hash. But I dont understand what happens if there are global scoped array variables in parent class. How do i access them or set them.

Replies are listed 'Best First'.
Re: Data inheritance
by ikegami (Patriarch) on Sep 23, 2011 at 18:17 UTC

    I think you're asking how to store an array to the object.

    • There's no reason to override the parent's object construction.

    • If you are using hash based objects, then you can't store an array into it directly. Hashes can only have scalars for values. You could store a reference to an array, though. [] creates an array and returns a reference to that array.

    • You're getting a syntax error because that's not even close to how one assigns to a hash via a reference.

    package Base; use strict; use warnings; sub new { my $class = shift; return bless({}, $class); } 1;
    package Child; use strict; use warnings; use Base; our @ISA = qw( Base ); sub new { my $class = shift; my $self = $class->SUPER::new(); $self->{nums} = [qw( 1 2 )]; return $self; } 1;
      So this code isnt working..
      CommonObject.pm my $some_var; #constructor sub new{ my $class=shift; my $self = {} ; bless $self, $class; print Dumper($self); return $self; } MyObject.pm our @ISA = qw( CommonObject ); sub new{ my $class=shift; $testName=shift; my $self = CommonObject->new(); $self->some_var="abcd"; return $self; }
      I want to be able to set some_var here. I get error Global symbol "$some_var" requires explicit package name. Isnt inheritance supposed to get the variable into MyObject??

        The code you posted does not produce that error.

        The code you posted fails for a different reason: You are trying to assign a scalar to a method call ($self->some_var="abcd";).

Re: Data inheritance
by ikegami (Patriarch) on Sep 23, 2011 at 18:26 UTC

    ...Then again, I see that you actually have my @common_array;. If that's what you want to set, ignore objects and classes. That variable has nothing to do with either.

    If all the code is in the same file, you can simply do @common_array = qw(1 2); since you are still in the same lexical scope.

    If the definition (my @common_array;) and the code from which you want to access @common_array are in separate files, then you can't access the array from that code since the array is outside of the scope of the code in question.

    You could work around that problem by using a package variable (our @common_array;) instead of a lexical variable (my @common_array;). Package variables are globally scoped (if you use their full name), which means you can access the array as @CommonObject::common_array from anywhere.

      Hi ikegami, Thank you for yor replies. The packages are not in the same file. CommonObject is meant to be used by multiple child class objects. I just put all the common variables in the CommonObject.

      So the question is, which is better:

      Make the array variable a ref variable and do $self->$common_array_ref=1,1;

      or make it package scope variable.

        Between the two options you gave, I would go with the one that compiles.

        Of course, it's not between the two options you gave. What's with using global variables, especially one that gets clobbered every time you create an object.

Re: Data inheritance
by SuicideJunkie (Vicar) on Sep 23, 2011 at 18:21 UTC
    my $self = {}; # is a hash ref $self->{foo}; # element foo of that hash $self->{foo} = ['bar', 'baz']; # assign an arrayref to that element @{$self->{foo}}; # element of the hash, dereferenced as +an array. @{$self->{foo}} = (1,2); # assign (1, 2) to the array. push @{$self->{foo}}, (3); # array now contains 1,2,3

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://927588]
Approved by philipbailey
Front-paged by philipbailey
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-19 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found