Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How do YOU do OO in Perl?

by vadim_t (Acolyte)
on Oct 07, 2003 at 13:17 UTC ( #297277=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: How do YOU do OO in Perl?
Select or Download Code
Re: Re: How do YOU do OO in Perl?
by PodMaster (Abbot) on Oct 07, 2003 at 13:30 UTC
    s'$self->{$pkg} = {};'$self->{ + __PACKAGE__ } = {};';
    update: but it's not prettier ;)(1 teenie-weenie + versus another variable? come on)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      $pkg is the same as __PACKAGE__, look above. I just use $pkg to make it shorter and prettier.
      I'd prefer $self->{(__PACKAGE__)}. One more character, but it doesn't look so noisy.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-09-18 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (126 votes), past polls