Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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        


In reply to Re: OO - best way to have protected methods (packages) by tye
in thread OO - best way to have protected methods by gargle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found