Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How do YOU do OO in Perl?

by hardburn (Abbot)
on Oct 06, 2003 at 17:49 UTC ( #297014=note: print w/ replies, xml ) Need Help??


in reply to How do YOU do OO in Perl?

. . . (e.g. $self->{member}). As Abigail-II pointed out, though, this can make misspellings into tricky runtime errors.

Just thought of this: Create scalars with package-wide scope to hold the key names of your attributes. For instance:

package Foo; use strict; my $attr_bar = 'bar'; my $attr_baz = 'baz'; sub new { # Constructor as normal } sub bar { my $self = shift; $self->{$attr_bar} = shift if @_; $self->{$attr_bar}; } 1;

This makes your data available to subclasses and will create errors under use strict if you misspell the variable used to access the hashref. It's probably best if you make the variable names and key names different, in case you forget to add the $ sigal.

I don't know a way around the "uglyness", except to wait for Perl6, which should have a much nicer object system.

Anyway, on to your actual question.

Most of my objects are blessed hashrefs. Occasionally, I'll use a closure or simple class methods. Since Perl has so many ways of doing objects, and the term 'Object Oriented' is rather broadly defined, I suspect I've used a bunch of different object systems without even noticing it.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated


Comment on Re: How do YOU do OO in Perl?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (18)
As of 2014-07-30 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (234 votes), past polls