Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: OO - best way to have protected methods (packages)

by tye (Sage)
on Aug 17, 2005 at 14:43 UTC ( [id://484450]=note: print w/replies, xml ) Need Help??


in reply to OO - best way to have protected methods

package My::Class::_Implementation; sub _add { my $me= shift @_; # ... } sub _init { my $me= shift @_; # ... } *My::Class::instancePackage= sub { my $class= shift @_; return $class . "::Object"; }; *My::Class::new= sub { my $us= shift @_; my $me= bless {}, $us->instancePackage(); _init( $me ); return $me; }; *My::Class::Object::new= sub { my $me= shift @_; my $new= bless {}, ref($me); _init( $me ); return $me; }; *My::Class::Object::method= sub { my $me= shift @_; _add( $me, @_ ); };

If you don't like the "*My::Class::..." part, then you can instead export your methods to My::Class and/or My::Class::Object depending on whether they are class methods or object methods. Lots of ways to do that.

This technique prevents using a class method on an object, prevents using an object method on the class, makes it easy for you to use your private subroutines, makes it very clear that they are private, but doesn't prevent someone from getting their job done when they realize that your class design doesn't allow for them to do their job portably so they need to implement a hack that does something with your private subs that will likely break in some future version of your module (while not having to modify your module which might be being used portably elsewhere).

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-24 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found