Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Inheritance and Internal object methods

by Angel (Friar)
on Jun 29, 2003 at 21:32 UTC ( [id://270025]=perlquestion: print w/replies, xml ) Need Help??

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

I have a whole bevy of objects and have been learning recently how to do inheritance, my problem has to do with calling internal methods since they dont get the $self as their first argument so things like accesing the object hash fail.

#assume new method has been called sub foo # the public method { my $self = shift; &bar( "do something" ); } sub bar # the private method { $self{new} = $_[0]; }

This does not seem to work what I would like to learn is how to get at the $self object from inside bar hopefully without passing it as a calling argument and is there anything that needs to be done about calling a method in a parent class?

Replies are listed 'Best First'.
Re: Inheritance and Internal object methods
by dws (Chancellor) on Jun 29, 2003 at 21:52 UTC
    ... my problem has to do with calling internal methods since they dont get the $self as their first argument ...

    If you use a normal (non-OO) subroutine invocation, then you're invoking a subroutine, not a method. It's fine for a package to include subroutines as well as methods, though it makes the code harder to subclass and extend.

    Since you seem to want access to $self from within the private method, you probably want to do something like

    sub _bar { my $self = shift; ... }
    and invoke it from your public method via
    $self->_bar("do something");
    Note that I've written _bar instead of bar. That's a convention that some use to designate that a method is private.

Re: Inheritance and Internal object methods
by fglock (Vicar) on Jun 29, 2003 at 21:45 UTC

    bar() will get $self if you call it like this:

    sub foo { my $self = shift; $self->bar( "do something" ); }

    If foo() had to call a method in the parent class:

    sub foo { my $self = shift; $self->SUPER::foo( "do something" ); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-26 02:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found