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

Re^2: Data inheritance

by perlbaski (Acolyte)
on Sep 23, 2011 at 21:00 UTC ( #927615=note: print w/ replies, xml ) Need Help??


in reply to Re: Data inheritance
in thread Data inheritance

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


Comment on Re^2: Data inheritance
Download Code
Re^3: Data inheritance
by ikegami (Pope) on Sep 23, 2011 at 21:04 UTC

    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";).

      It does, I had use strict and use warnings in MyObject.pm :(
        Even with those. You did not post the code you ran.
      Hey..yes..that was a typo..i cant copy paste my code here. I had $self->$some_var="abcd";#This was not the problem.
      package CommonObject; my $some_var; #constructor sub new{ my $class=shift; my $self = {} ; bless $self, $class; print Dumper($self); return $self; } 1; package MyObject; our @ISA = qw( CommonObject ); use strict; use warnings; sub new{ my $class=shift; my $testName=shift; my $self = CommonObject->new(); $self->$some_var="abcd"; print $self->$some_var; return $self; } 1; #!/usr/bin/perl use MyObject; my $obj=MyObject->new("lkj");
      Thats the exact code
        Of course that doesn't work. That's the same as your original code, just with a scalar instead of an array. Goto Re: Data inheritance
      Then why is it called inheritance?? Sorry for the persistent naive questions..
        It's not, it's a called a lexical variable.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-08-30 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls