Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

inheritance problems

by michaelg (Beadle)
on Aug 16, 2004 at 12:35 UTC ( [id://383279]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks
I'm trying to write code of Obj inheritance but I face some problems:
my $jhon = Worker->new_worker(); $jhon->print_worker_data(); my $telma = Worker->new_worker(name => "Telama", age => 25); $telma->print_worker_data(); my $bob = Manager->new_worker(name => "Bob", age => 40); $bob->print_worker_data(); $bob->wc(); ##===================================== package Worker; sub new_worker { ## Constructor. my $invocant = shift; my $class = ref($invocant); my $self = { name => "Jhon Do", age => 32, @_ , }; bless ($self) ; return $self ; }; sub print_worker_data { my $self = shift; print $self->{name} . " age is:". $self->{age}."\n"; }; ##=========================================== package Manager; use base ("Worker"); sub wc{ my $self = shift; print $self->{name} . " is in title to use the managers toilet\n"; + };
OK, I started the Manager pack with "our @ISA = "Worker" but I couldn't do any thing for Bob there, When I wrote "use base ("Worker")" I was able to initiate Bob but I could't activate the wc method. So Bob need to go out with the common people. Thanks guys
Michael

Replies are listed 'Best First'.
Re: inheritance problems
by Corion (Patriarch) on Aug 16, 2004 at 12:44 UTC

    You are using the single-argument form of bless, which does not bless $bob into the class Manager:

    my $bob = Manager->new_worker(name => "Bob", age => 40); print $bob,"\n"; # Worker=HASH(0x15debec)

    To properly enable inheritance, you must also properly bless every object into the right class. I don't know where you copied your constructor from, but you shouldn't throw away the package name to use (you put it into $invocant), but pass it to bless as the second argument:

    sub new_worker { ## Constructor. my $invocant = shift; # my $class = ref($invocant); my $self = { name => "Jhon Do", age => 32, @_ , }; bless $self, $invocant; return $self; };

    Also, you should consider renaming your new_worker method to new, as is customary with constructors.

Re: inheritance problems
by dragonchild (Archbishop) on Aug 16, 2004 at 12:46 UTC
    Your immediate problem is that you're using the one-argument form of bless. You need to use the two-argument form to get inheritance working. (The second argument defaults to the package the constructor is in.)

    Your real problem is that you are confused about what's going on in a Perl constructor. I would have written your constructor like so:

    sub new_worker { my $class = shift; die "Use the copy constructor!\n" if ref $class; my $self = { name => "John Doe", age => 32, @_ , }; return bless $self, $class; }

    You don't need any ref() crud in your constructor. That is cargo-cult programming and merlyn has posted enough diatribes about it, as have many other monks. If you want a copy-constructor, then create a clone() method. (You'll notice I don't allow this method to be a copy-constructor.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: inheritance problems
by Joost (Canon) on Aug 16, 2004 at 12:45 UTC
    ccn's solution will work, but in the long run, it's probably a better idea to put the Worker and Manager packages in their own module files (Worker.pm and Manager.pm) and use Worker; and use Manager;.

    That will make sure all setup code is run before any calls to those packages are made. (The problem with your code is that Manager->new_worker() is called before @Manager::ISA is set)

    Joost.

    update: and use bless {}, $classname;

Re: inheritance problems
by ccn (Vicar) on Aug 16, 2004 at 12:39 UTC

    Try out to enclose your @ISA setup in BEGIN{} block

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (9)
As of 2024-04-16 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found