Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How do YOU do OO in Perl?

by hardburn (Abbot)
on Oct 06, 2003 at 17:49 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found