in reply to How do YOU do OO in Perl?
I use normal hash blessing with a small improvement for avoiding bugs due to inheritance. Here's an example:
package Foo::Bar; my $pkg = __PACKAGE__; sub new { my ($class) = @_; my $self = {}; bless($self, $class); $self->{$pkg} = {}; return $self; } sub setvar { my $self = shift; my $priv = $self->{$pkg}; $priv->{variable} = 1; }
Each method has two variables, $self, which is the blessed hash and is used to call methods, and $priv, which is used to keep the private variables. So, if you use this class, 'variable' will be $self->{Foo::Bar}->{variable}. If a class using the same technique, say, Foo::Bar::Improved also defines the same variable, and inherits from Foo::Bar, then the variable be $self->{Foo::Bar::Improved}->{variable}. No conflict.
You won't see that in the module I posted here though, because it was written a long time ago when I didn't do this yet.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: How do YOU do OO in Perl?
by PodMaster (Abbot) on Oct 07, 2003 at 13:30 UTC | |
by vadim_t (Acolyte) on Oct 07, 2003 at 13:57 UTC | |
by Aristotle (Chancellor) on Oct 10, 2003 at 00:20 UTC |
In Section
Meditations