Oh great monks...

Thanks for these responses, they are very interesting. But I can't seem to convince myself that there's no way for a parent class to access it's own methods without being explicitly passed a reference to an object of it's type.

An additional puzzle: why doesn't this work?
if ( ref($self) eq 'person') { $self->tellName(); } if ( ref($self) eq 'child') {$self->SUPER::tellName();}
When I put that into person::introduce, I get this:
Hello, I'm Alice. Hi, I'm Billy The Kid. Can't locate object method "tellName" via package "person" at ./classt +est line 25. Here's my mom: Hello, I'm

Which seems to tell me that even when $self was passed in from a child object, it's trying to locate SUPER::tellName inside of package "person".

Am I interpreting that correctly?

Complete code with above modification:
#!/usr/bin/perl use strict; package person; sub new { my $class = shift(@_); my $self = { 'name' => 'Alice' }; bless($self, $class); return($self); } sub tellName { my($self)=$_[0]; print "$self->{'name'}.\n"; } sub introduce { my($self)=$_[0]; print "Hello, I'm "; # $self->tellName(); # original code # tellName($self); # one solution offered by perlmonks #print "\ndebug info: " . ref($self) . "\n"; if ( ref($self) eq 'person'){ $self->tellName(); } if ( ref($self) eq 'child') { $self->SUPER::tellName();} } 1; package child; our @ISA = qw(person); # inherits from person sub new { my $class = shift(@_); my $self = $class->SUPER::new(); $self ->{'nickName'} = 'Billy The Kid'; bless $self, $class; return $self; } sub tellName { my($self)=$_[0]; print "$self->{'nickName'}.\n"; } sub introduce { my($self)=$_[0]; print "Hi, I'm "; $self->tellName(); print "Here's my mom: "; $self->SUPER::introduce(); } 1; package main; my $mom = new person(); $mom->introduce(); # that time it works. print "\n"; my $son = new child(); $son->introduce();

In reply to Re: my $self is stumped! by headybrew
in thread my $self is stumped! by headybrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.