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

Re: using passed parent object reference to get at attributes

by GrandFather (Saint)
on Dec 20, 2015 at 00:34 UTC ( [id://1150782]=note: print w/replies, xml ) Need Help??


in reply to using passed parent object reference to get at attributes

Not sure what you are trying to do, but the following may suggest some things to think about. You may also be interested in Not quite an OO tutorial for a light weight introduction to Perl OO.

use strict; use warnings; package cParent; sub new { my ($class) = @_; my $self = bless {test_att => 3}, $class; return $self; } sub parent_sub { my ($self) = @_; print "In parent. Test value is $self->{test_att}\n"; } package cChild; push @cChild::ISA, 'cParent'; sub child_sub { my ($self) = @_; print "In child_sub. Test value is $self->{test_att}\n"; } package main; my $obj = cChild->new(); $obj->child_sub(); $obj->parent_sub();

Prints:

In child_sub. Test value is 3 In parent. Test value is 3

If the reply is way off base maybe you need to tell us more about what you are trying to achieve.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: using passed parent object reference to get at attributes
by mr_ron (Chaplain) on Dec 20, 2015 at 14:50 UTC

    I wondered about doing "use parent" instead of working with @ISA for the test case. I didn't know but it turns out you can if the Perl isn't really ancient (I think you need 5.10.1). Instead of:

    push @cChild::ISA, 'cParent';
    one could also:
    use parent -norequire, 'cParent';

    In the real application, as opposed to the test example, the classes are probably broken up into seperate files and you probably just remove the '-norequire, '.

    Ron
      @ISA is something I knew nothing about so thanks for raising it as a possibility...I see it's "Each package contains a special array called @ISA . The @ISA array contains a list of that class's parent classes, if any. This array is examined when Perl does method resolution, which we will cover later. " Thank you for the education!
Re^2: using passed parent object reference to get at attributes
by previous (Sexton) on Dec 20, 2015 at 10:42 UTC
    Thank you for you're reply...and question re what I'm trying. The object within an object is a proxy for a combo box that I've made i.e. the list box is in the parent object and the child object is a TK entry widget that I've "enhanced" and that is useful in it's own right. The combo boxes will be used in clusters and whilst there's some similarity in their functionality (locked away in the objects) I wanted to customise them by passing in a non-oop subroutine that gets called when a key is pressed in the entry (child object). To this end I'm passing the non-oop customising subroutine into the parent object to pass onto the child object upon it's creation along with a reference to the parent object. This is so the child object (entry) can call the custom subroutine and supply it with the parent object's reference when the entry's custom bindtag fires. I could reference the combo's directly 'cos they created in main just like the non-oop sub but wonder how you'd pass a combo reference internally...for future reference i.e. I can get around this but in a not very oop-way. Hope that explains

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found