Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Data inheritance

by ikegami (Patriarch)
on Sep 23, 2011 at 18:26 UTC ( [id://927595]=note: print w/replies, xml ) Need Help??


in reply to Data inheritance

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

Replies are listed 'Best First'.
Re^2: Data inheritance
by perlbaski (Sexton) on Sep 23, 2011 at 18:44 UTC
    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.

        Is there any other way of doing this apart from using global variables. I want to have common data variables in CommonObject.pm which I want to set in child class depending upon the type of child I create.

        Also, I cannot seem to be able to set parent class variables in child class even with declaration using 'our'.

        Looks like using exporter module is the only way to go.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://927595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found