Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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

by tye (Sage)
on Aug 17, 2005 at 14:43 UTC ( #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?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2022-05-27 13:08 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (95 votes). Check out past polls.