Beatnik has asked for the wisdom of the Perl Monks concerning the following question:
So I have 2 packages, foo inherits from bar. In the bar package, I have a method using __PACKAGE__ to get the package name. I instanciate a foo object and call 2 available methods. Description returns the foo description, the id method returns the package name from bar.package bar; # base class. Has a description method and an id method sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{DESCRIPTION} = "Bar!!!"; bless($self,$class); return $self; } sub description { my $self = shift; return $self->{DESCRIPTION}; } # return a description sub id { return __PACKAGE__; } # return the package name package foo; # inheriting from bar package, not overriding the id method. @ISA = qw(bar); use bar; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = $class->SUPER::new; $self->{DESCRIPTION} = "Foo!!"; # yes I'm lazy :) bless($self,$class); return $self; } package main; use foo; $foo = new foo; print "Description is ",$foo->description,"\n"; print "Package is ",$foo->id,"\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: __PACKAGE__ interpolated before inheritence
by wog (Curate) on Dec 08, 2001 at 19:22 UTC | |
by Beatnik (Parson) on Dec 08, 2001 at 19:32 UTC | |
by wog (Curate) on Dec 08, 2001 at 19:45 UTC | |
by blakem (Monsignor) on Dec 09, 2001 at 01:44 UTC |